Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
    
Try to replace your first elseif statement with elseif ( false ) ... and see if you ever reach your second one. You should also paste the code for your custom is_category_or_sub function. –  kovshenin Oct 16 '12 at 8:26
    
When I replace it with elseif (false), it reaches the second one. Both the gamenews and hobbynews now display content meant for hobbynews. Ok, I'll edit the question. –  deathlock Oct 16 '12 at 8:56
    
It is edited now –  deathlock Oct 16 '12 at 8:58
    
can you check switching the hobbynews & gamenews if conditions –  Mridul Aggarwal Oct 16 '12 at 9:02
    
@MridulAggarwal when I switch the place, what happens is the same like when I do what kovshenin told me to do. The second one's content shows up on both categories, while the first one's content does not show up. –  deathlock Oct 16 '12 at 9:10

1 Answer 1

up vote 1 down vote accepted

Assuming you're using the function is_category_or_sub() as given on http://valendesigns.com/wordpress/is-category-or-subcategory-wp-function/

That function takes category id as the argument, not the slug. The string "gamenews" here when type-casted to an integer becomes 0 which is an ancestor for every other category. Can you try changing the function code to this-

// If is category or subcategory of $cat
if (!function_exists('is_category_or_sub')) {
    function is_category_or_sub($cat = 0) {
            if(is_numeric($cat)) {
                $cat_id = $cat;
            } elseif(is_string($cat)) {
                $cat = get_term_by('slug', $cat, 'category');
                $cat_id = $cat->term_id;
            } else {
                return false; // neither id nor slug
            }
            foreach (get_the_category() as $cat) {
                if ($cat_id == $cat->cat_ID || cat_is_ancestor_of($cat_id, $cat)) return true;
            }
            return false;
    }
}

This function now supports both id & slug

share|improve this answer
    
Yes, I indeed base my code on Valendesign's, however I modified it so it could read the slug. The first elseif (gamenews) works perfectly fine. The only problem lies on the second elseif (hobbynews). –  deathlock Oct 16 '12 at 8:55
    
I have edited my question to include my is_category_or_sub, please check it out :) –  deathlock Oct 16 '12 at 8:59
    
I feel so incredibly stupid as to WHY DIDN'T I use your function instead of persisting on mine. Once I change my is_category_or_sub to the one you've wrote, it works magically! Really wonderful. I still don't understand though, why is your code working and mine does not? Mostly because of my lack of programming expertise, but if an explanation is allowed I'd really be thankful. –  deathlock Oct 16 '12 at 16:07
2  
The "if ($cat_slug" part will always return true, when $cat_slug is a non-empty string. –  Otto Oct 16 '12 at 17:57
    
glad you got it working –  Mridul Aggarwal Oct 16 '12 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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