普通网友 2015-10-22 21:52
浏览 35
已采纳

WordPress:设置特定模板的主体类

I created a specific template for one page only = I don't want it to be listed in the admin section. So I removed the "Template name" from the comment and let WP figure it out by the slug of the post. That works. Now I would like to assign a custom body class for this template only (as I do with every page) in my functions file:

function cc_set_body_classes($classes) {
    switch(true) {
        case is_page_template("page-about-us.php"):
            $classes[] = "about-us";
            break;
    }

    return $classes;
}
add_filter("body_class", "cc_set_body_classes");

Now this would obviously work just fine if it wasn't a specific template without the Template name. Is it possible to figure out the template name if no Template name is specified in the comments? How should I do it? Thanks a lot!

  • 写回答

1条回答 默认 最新

  • dpqvwgr74759 2015-10-22 22:01
    关注

    You could always check the page slug (since that's what you've used to define the template name in the first place). Something like the following should work:

    function cc_set_body_classes($classes) {
        if ( get_queried_object()->post_name === "about-us" ) {
            $classes[] = "about-us";
        }
    
        return $classes;
    }
    add_filter("body_class", "cc_set_body_classes");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)