WordPress:根据显示的页面向正文添加动态类

我需要在以下路线下的特定页面上添加一个类:www.mydomain.com/special /...

因此,每当/ special /在当前URL中时,我都认为可以将一个类添加到主题header.php文件中:

<body id="landing-page" <?php body_class(); ?>>

这是走的路吗?如何获取当前页面的网址?

xuanyuanwsx 回答:WordPress:根据显示的页面向正文添加动态类

您可以将以下代码段添加到您的functions.php

add_filter( 'body_class','custom_class' );
function custom_class( $classes ) {
    if ( is_page('special') ) {
        $classes[] = 'myclass';
    }
    return $classes;
}

add_filter( 'body_class','custom_class' );
    function custom_class( $classes ) {
        if(strpos($_SERVER['REQUEST_URI'],"special") !== false){
            $classes[] = 'myclass';
        }
        return $classes;
    }
本文链接:https://www.f2er.com/3150501.html

大家都在问