WordPress.org

Forums

Filter out sidebars on specific category posts, and some specific pages (6 posts)

  1. David Borrink
    Member
    Posted 1 year ago #

    When certain single posts in a specific category get called up, I'd like to remove the sidebars and go to a full width content area.

    Here is the function I've worked on, but it doesn't remove the sidebar, nor make the content 100% wide. It does remove the post footers and comment areas, though which is not what I wanted and I can't understand why.

    Any ideas why this isn't working? This is on a TwentyTwelve child theme. I'm on a development site, so there's no link available.

    function product_posts_no_sidebar() {
     global $post;
     if ( !is_category() ) return;
     $cats=get_the_category($post->ID);
     foreach($cats as $cat) {
     if( in_array($cat->cat_name,array("store-all")) ) {
     {?>
     <style>
     #secondary { display: none;}
     .site-content  {width:100%;}
     </style>
     <?php }
     }
     }
    }
    add_filter('single_template', 'product_posts_no_sidebar');
  2. David Borrink
    Member
    Posted 1 year ago #

    I see the list of template filters on the codex, is there one that covers any kind of post page. I find the descriptions to be a little lacking in detail.

  3. David Borrink
    Member
    Posted 1 year ago #

    Any ideas why this would be dropping the comments area on all single post pages?

  4. David Borrink
    Member
    Posted 1 year ago #

    Okay, let's try this... a completely different code write-up, and this one does remove the sidebar and makes the content 100%, BUT... it does it on all posts and pages. I'm using "the_content" as my filter hook this time.

    add_filter('the_content', 'product_posts_no_sidebar');
    
    function product_posts_no_sidebar() {
    if ( in_category('store-all') )
     ?>
     <style>
     #secondary { display: none; }
     .site-content  { width:100%; }
     </style>

    So I'm closer, but I'm apparently not setting it up correctly to happen only on single post pages where "store-all" is one of the categories.

  5. David Borrink
    Member
    Posted 1 year ago #

    By the way, I will also want this to happen on select pages, too, and not just on some single page posts.

  6. David Borrink
    Member
    Posted 1 year ago #

    Ah, it IS "single_template" to make this work.

    I've done this:

    add_filter('single_template', 'product_posts_no_sidebar');
    
    function product_posts_no_sidebar() {
    if ( in_category('store-all') )
     ?>
     <style>
     #secondary { display: none; }
     .site-content  { width:100%; }
     </style>

    and the sidebar disappears, but the content is still at the default width for my site. I discovered looking at my CSS in Firebug that the original site's width is applied, while my function's width is there, but it's crossed out. I just need to figure out how to get my function's style to be accepted as the correct one instead of my style.css's setting.

    But I've checked other posts that do not have "store-all" for a category, and those also have the sidebar missing. So this category call-out in my function is not working as I want it to.

Topic Closed

This topic has been closed to new replies.

About this Topic