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 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?