【问题标题】:Add CSS Style to Wordpress Sidebar on Article Page将 CSS 样式添加到文章页面上的 Wordpress 侧边栏
【发布时间】:2016-07-06 20:24:39
【问题描述】:

我需要在我的网站上的文章和单页侧边栏中添加一个独特的 CSS 样式,以区别于主页。侧边栏已经有一个名为“.sidebar”的样式,当我更改它时它会起作用,但它会影响所有站点侧边栏。

我一直在查看 sidebars.php 文件,发现主页或单个页面有“if”行,但不知道要更改什么。请记住,我不是程序员。

我认为应该修改的代码是(sidebar.php):

<aside class="sidebar">
<?php
    wp_reset_query();
    if ( is_home() ){

        $sidebar_home = tie_get_option( 'sidebar_home' );
        if( $sidebar_home )
            dynamic_sidebar ( sanitize_title( $sidebar_home ) ); 

        else dynamic_sidebar( 'primary-widget-area' );  

    }elseif( is_page() ){
        global $get_meta;
        $tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];

        if( $tie_sidebar_pos != 'full' ){
            $tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
            $sidebar_page = tie_get_option( 'sidebar_page' );
            if( $tie_sidebar_post )
                dynamic_sidebar($tie_sidebar_post);

            elseif( $sidebar_page )
                dynamic_sidebar ( sanitize_title( $sidebar_page ) ); 

            else dynamic_sidebar( 'primary-widget-area' );
        }

    }elseif ( is_single() ){
        global $get_meta;
        $tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];

        if( $tie_sidebar_pos != 'full' ){
            $tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
            $sidebar_post = tie_get_option( 'sidebar_post' );
            if( $tie_sidebar_post )
                dynamic_sidebar($tie_sidebar_post);

            elseif( $sidebar_post )
                dynamic_sidebar ( sanitize_title( $sidebar_post ) ); 

            else dynamic_sidebar( 'primary-widget-area' );
        }

    }elseif ( is_category() ){

        $category_id = get_query_var('cat') ;
        $cat_sidebar = tie_get_option( 'sidebar_cat_'.$category_id ) ;
        $sidebar_archive = tie_get_option( 'sidebar_archive' );

        if( $cat_sidebar )
            dynamic_sidebar ( sanitize_title( $cat_sidebar ) ); 

        elseif( $sidebar_archive )
            dynamic_sidebar ( sanitize_title( $sidebar_archive ) );

        else dynamic_sidebar( 'primary-widget-area' );

    }else{
        $sidebar_archive = tie_get_option( 'sidebar_archive' );
        if( $sidebar_archive ){
            dynamic_sidebar ( sanitize_title( $sidebar_archive ) );
        }
        else dynamic_sidebar( 'primary-widget-area' );
    }
?>
</aside>

提前感谢您提供解决此问题的任何帮助。

【问题讨论】:

  • 文章是一个页面。??

标签: php css wordpress sidebar custom-wordpress-pages


【解决方案1】:

您的文章应该有一个唯一的页面或帖子 ID,请查看页面/帖子源代码以获取 id。你的 CSS 看起来像 #post_455 .sidebar { /* your unique article styles */ }

ps - 你不需要玩 sidebar.php

【讨论】:

  • 感谢尼克的回答。我试过你的建议,但没有奏效。此外,如果这样做,为每个文章 ID 创建样式将太乏味。我发现生成的html看起来像:
  • 是否可以添加如下样式: .single-post .sidebar {CSS} 因为我也尝试过,但没有成功。
  • 抱歉,我再次阅读了您的问题。如果这是您想要看起来不同的唯一页面,请向主页添加独特的样式。主页将有一个唯一的 id 或类。
  • 伟大的尼克。我注意到每种类型的页面都有自己的 ID,所以我做了这样的事情:body.single-post .sidebar {margin-top: 250px !important;} 效果很好。非常感谢您指出正确的方向。
猜你喜欢
  • 2011-04-14
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多