Leaky Eddie
Leaky Eddie

Reputation: 117

I need if (is_category ( )) to display content ONLY on parent category, not on child category pages

I need to display an intro paragraph on an archive page that displays all posts in the parent and child categories. I'm adding the required content via a sidebar widget and I have code that displays it correctly.

if( is_category( '28' ) || get_sidebar('blog-intro'));

The problem is the side bar widget also displays on all child pages for category 28 => 28/a, 28/b,... I need it on just category 28. Any ideas on how I can filter that out?

Upvotes: 1

Views: 1633

Answers (3)

Thomas
Thomas

Reputation: 1079

I'd look into get_category()

If I recall correctly, $cat->parent_category() will return 0 if it's a top level category

So:

if( get_query_var( 'cat' ) == 28 ) {

     $cat = get_category( 28 );

     if( ! $cat->parent_category() ) {

         // output content here

     }

}

Upvotes: 0

ambroseya
ambroseya

Reputation: 31

The actual category id of a category archive is in the global $cat.

if(is_category()){
  global $cat;
  if($cat == '28'){
    //do stuff here
}

Upvotes: 1

Leaky Eddie
Leaky Eddie

Reputation: 117

On further lowering of expectations, I think allowing the widget to display on all blog archive pages but not on single posts is acceptable.

But I'm still curious about the answer.

Upvotes: 0

Related Questions