douhan9748 2012-06-27 18:25
浏览 42
已采纳

试图将the_slug添加到我的身体类而在wordpress中没有成功,有人可以建议吗?

Using the following code to try and add the page slug as a class to my body tag but as yet having no success, could anyone advise how I might achieve this?

PHP added in functions.php

function add_body_class( $classes )
{
    global $post;
    if ( isset( $post ) ) {
        $classes[] = $post->post_type . '-' . $post->post_name;
    }
    return $classes;
}
add_filter( 'body_class', 'add_body_class' );
  • 写回答

3条回答 默认 最新

  • dtjwov4984 2012-06-27 18:43
    关注

    You can try adding the following to your functions.php:

    function page_bodyclass() {  // add class to <body> tag
        global $wp_query;
        $page = '';
        if (is_front_page() ) {
               $page = 'home'; // If it's your Front page, apply 'home' as classs
        } elseif (is_page()) {
           $page = $wp_query->query_vars["pagename"]; // Otherwise use pagename
        }
        if ($page)
            echo 'class= "'. $page. '"';
    }
    

    Then change your <body> tag to <body <?php page_bodyclass(); ?>>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?