0

I need some way to hide theme built in widget from one category.So when the post is from category X on that post hide that widget. I would use plugin for it, but since it's not drag and drop widget I can't. It's a ticker list which is built inside the theme. I have tried with <?php if(!in_category('X')) : ?> like I disabled one other, but this is <ul> list, and my solution ended with error.

This is the code with example what I've tried, and even tried to change position of this in_category() but no luck. Perhaps it needs to be done inside theme function with filter but i didnt find a way or code for it.

The code in header.php:

<div id="ticker">
<span class="ticker-heading"><?php _e( "Don't Miss", 'mvp-text' ); ?></span>
<ul class="ticker-list">
    <?php if(!in_category('X')) : ?><?php else : ?><?php $recent = new WP_Query(array( 'tag' => get_option('gd_ticker_tags'), 'showposts' => get_option('gd_ticker_num') )); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>                       
</div><!--ticker-->
  • This line of code seems to do it, but also it removes it from Homepage and every cetegory page acrhive. So i am currently on 50-50 . I V tried with singular and changing place of ! and is / in_ – Robert Sep 18 '13 at 14:42
1

I think you might mean is_category()?

Codex: is_category()

0

I totally agree with Wrought_steel's answer, that's the correct way. So the correct way is:

is_category()

And your refined code will be:

<div id="ticker">
<span class="ticker-heading"><?php _e( "Don't Miss", 'mvp-text' ); ?></span>
<ul class="ticker-list">
    <?php if(!is_category('X')) : //just to hide something from Cat = X ?>
         <?php $recent = new WP_Query(array( 'tag' => get_option('gd_ticker_tags'), 'showposts' => get_option('gd_ticker_num') ));
         while($recent->have_posts()) : $recent->the_post(); ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
         <?php endwhile; ?>
    <?php endif; ?>
</ul>                       
</div><!--ticker-->

NO NEED to use an else condition, as you want something to hide if it's NOT the desired category.

  • Just seen what i wrote and edited my question, it was really late for me.What i want is, if the post is inside category X than on that post hide that widget. Thanks – Robert Sep 18 '13 at 13:00
  • What's the result when you tried mine? if(!is_category('X'))? Because is_category() returns boolean, it should have worked. If you want to show the <li> only in this category, just remove the naught (!) from the condition. Let me know what's the result if I'm on the right way. – Mayeenul Islam Sep 19 '13 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.