I have this on my header.php
.
It checks whether the page is on homepage/frontpage, is on category/children of/has the category of "gamenews", or is on category/children of/has the category of "hobbynews".
Basically, if it's on homepage/frontpage, it outputs nothing.
On the other hand, if it's on gamenews, it outputs something. If it's on hobbynews, it outputs another thing. You could see in details below. I'm using a custom function there (is_category_or_sub
).
<?php if ( is_home() || is_front_page() ) { ?>
<?php } elseif ( is_category_or_sub('gamenews') || has_category('gamenews') ) { ?>
<ul id="sections">
<?php $category = get_category_by_slug( 'gamenews' );
wp_list_categories(array(
'child_of' => $category->term_id,
'title_li' => '',
'hide_empty' => 0,
'orderby' => 'count',
'walker' => new WPSE67791_Walker_Category
)); ?>
</ul><!-- #sections -->
<?php } elseif ( is_category_or_sub('hobbynews') || has_category('hobbynews') ) { ?>
<ul id="sections">
<?php $category = get_category_by_slug( 'hobbynews' );
wp_list_categories(array(
'child_of' => $category->term_id,
'title_li' => '',
'hide_empty' => 0,
'orderby' => 'count',
'walker' => new WPSE67791_Walker_Category
)); ?>
</ul><!-- #sections -->
<?php } ?>
Now the first check (is_home/is_frontpage) works just fine. So does the second (the one on "gamenews").
However, the last one ("hobbynews") doesn't work.
Each time I visit page which has that requirement (is on category/children of/has the category of "hobbynews"), it displays stuff which is supposed to be on gamenews. The one I want to display on hobbynews does not show up.
Why is this happening? I've been trying to re-order, tried to use nested if, etc. But it's still not working and really confusing...
EDIT:
This is my is_category_or_sub
, located on functions.php
:
function is_category_or_sub( $cat_slug = get_category_by_slug ) {
foreach (get_the_category() as $cat) {
if ($cat_slug || cat_is_ancestor_of($cat_slug->term_id, $cat)) return true;
}
return false;
}
elseif ( false ) ...
and see if you ever reach your second one. You should also paste the code for your customis_category_or_sub
function.elseif (false)
, it reaches the second one. Both the gamenews and hobbynews now display content meant for hobbynews. Ok, I'll edit the question.