WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hide Sidebar if in Category (12 posts)

  1. kennethwatt
    Member
    Posted 5 years ago #

    Hi
    I have some static widgets in the sidebar.php, and a widget in the dynamic sidebar. I want to hide the dynamic sidebar if the post is in the category "artists". Does the following code not work as it's not in the loop?

    <?php if (in_category('Artists')) { ?>
    No dynamic sidebar shown.
    <?php }else{ ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?><?php endif; ?>
    <?php } ?>

    The above code has been put in the sidebar.php.
    What's wrong?
    Thanks in advance,
    Kenneth

  2. zero2heromn
    Member
    Posted 5 years ago #

    I actually have almost the same question as you. I might as well ask it here rather than create another thread.

    How can I not display a sidebar when not on my main page. I.E. when displaying a blog post.

    It should be about the same procedure as above.

  3. Nazieb
    Member
    Posted 5 years ago #

    If not in the Loop, you need to specify the current post ID as the second parameter for in_category function.

    Perhaps this could help:

    <?php if (in_category('Artists'), $id) { ?>
    No dynamic sidebar shown.
    <?php }else{ ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?><?php endif; ?>
    <?php } ?>
  4. kennethwatt
    Member
    Posted 5 years ago #

    Sorry, stuck your code in - got the following error:
    Parse error: syntax error, unexpected ',' in /****/****/public_html/forecourt.org.uk/wp-content/themes/asusena/sidebar.php on line 44
    Is it because i'm not replacing the $id with an actual number?

  5. kennethwatt
    Member
    Posted 5 years ago #

    @zero2heromn
    Should it not just be:

    <?php if (is_front_page()) { ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
    <?php }else{ ?>
    No sidebar. Woohoo.
    <?php endif; ?>
    <?php } ?>
  6. kennethwatt
    Member
    Posted 5 years ago #

    That will just hide a dynamic sidebar though. Maybe put conditional tags in the bottom of pages where it says <?php get_sidebar(); ?>.

    Edit the code I gave so that it has <?php get_sidebar(); ?> where it calls the sidebar.

    Sorry for the multiple posts BTW.

  7. Nazieb
    Member
    Posted 5 years ago #

    Oh, sorry I put the $id in the wrong place.. My bad..
    Don't change the $id. I think the $id the ID of the current post.

    Try this:

    <?php if (in_category('Artists', $id)) { ?>
    No dynamic sidebar shown.
    <?php }else{ ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?><?php endif; ?>
    <?php } ?>
  8. kennethwatt
    Member
    Posted 5 years ago #

    Thanks Nazieb that's great :-)

    K

  9. kennethwatt
    Member
    Posted 5 years ago #

    Sorry, but I've just realised that it's not displaying on the home page.

    It's working fine everywhere else.

    Tried changing it to:

    <?php if (in_category('artists', $id)) { ?>
    <?php if (is_home()) ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
    <?php }else{ ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?><?php endif; ?>
    <?php } ?>

    But comes up with error.

    Sorry - can't give live URL, as company I'm developing for won't allow me.

    K

  10. kennethwatt
    Member
    Posted 5 years ago #

    Would it be best for me to create another if (is_home)?

    K

  11. Nazieb
    Member
    Posted 5 years ago #

    Try this:

    <?php if (in_category('Artists', $id) && !is_home()) { ?>
    No dynamic sidebar shown.
    <?php }else{ ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?><?php endif; ?>
    <?php } ?>
  12. sene1983
    Member
    Posted 4 years ago #

    hi,
    i wanna try this code,

    <?php if (in_category('Category', $id)) { ?>
    No dynamic sidebar shown.
    <?php }else{ ?>
    <?php   /* Widgetized sidebar, if you have the plugin installed. */
              if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?><?php endif; ?>
    <?php } ?>

    But, to which file i will add this code? To, index.php or page.php
    thanx for your help

Topic Closed

This topic has been closed to new replies.

About this Topic