dpwgzi7987 2014-01-13 08:55
浏览 28
已采纳

仅适用于单个帖子的风格

I want my single post to have a style different from the main style. I know we can include a custom CSS file in single.php but I do not have a single.php file in my theme!

I just want a custom id, like id="single", added to the single post (not in main page posts) and then I can write styles for it.

I found some PHP code but I don't know how I can use it, or if it is what I want or not...

if (is_single()) {
    $single_css_styling = get_post_meta($post->ID, 'single_css_styling', true);
    if (!empty($single_css_styling)) { ?>
<style type="text/css">
  <?php echo $single_css_styling; ?>
<style>
<?php } } 

Is there any way to do it?

  • 写回答

2条回答 默认 最新

  • dongtiao0279 2014-01-13 09:04
    关注
       <?php if (is_single()) {
          echo '<script type="text/javascript">
               jQuery(document).ready(function(){jQuery("body").addClass("single");});
            </script>';
        } ?>
    

    you can now class >> single is added to your body tag using that identity you can easily apply your css

    Note : jQuery file must added to your site for that

    Without jQuery

    add_filter('body_class','my_class_names');
    function my_class_names($classes) {
          if (is_single()){
        // add 'class-name' to the $classes array
        $classes[] = 'single';
        // return the $classes array
          }
        return $classes;
    }
    

    Please make sure in body tag of header.php

    <body <?php body_class($class); ?>>
    

    is putted or not

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

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题