简体   繁体   中英

How to add a body class

I have the following piece of code in my functions.php file. What i want to do is to add one more attribute (let's say — "extraclass") to my shortcode [template]. Attribute's value should be the name of the extra class and I want my code to add this class to the body tag. I know about the add_filter function, i've tried to use it but didn't succeed.

function templatefn($atts, $content = null) {
    extract(shortcode_atts(array(
        "content" => ""
    ), $atts));

    ob_start();
    get_template_part('content', $content);
    $ret = ob_get_contents();  
    ob_end_clean();  
    return $ret;   
}
add_shortcode('template', 'templatefn');

Thanks in advance!

在模板中,如果您使用body_class()函数为body元素提供不同的css类(基于条件样式的当前页面),则应该可以使用列出的示例对此进行过滤,以实现结果。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM