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 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私