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 created a child theme for a theme I purchased. The purchased theme is for restaurants wanting to display their menu. The theme accepts menu items as Posts, and groups them according to category. The theme only accepts 1 level of categories, not more complex menus with 2 levels of categories.

The purchased theme is built for 1 level hierarchies. e.g:

>Menu
>>>Breakfast
>>>Lunch
>>>Dinner

However, the restaurant I am working for has a more complex menu:

>Menu
>>>Breakfast
>>>>>>Food
>>>>>>Sides
>>>>>>Drinks
>>>Lunch
>>>>>>Small
>>>>>>Large
>>>>>>Sides

My child theme aims to support two levels of categories.

Here is the portion of code which might be causing me problems:

<?php $args=array(
        'orderby' => 'slug',
        'order' => 'ASC',
        'include' => '4, 5, 6, 27',
        //'exclude' => '3, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26',
        'child_of' => $menuCat
        );
        $categories=get_categories($args);
        foreach($categories as $category) {
                $catName = $category->name;
                $catName = strtolower($catName);
                $catName = str_replace (" ", "", $catName);
                $catName = str_replace ("&amp;", "", $catName);
                $catName = str_replace ("é", "e", $catName);
        ?>
        <div class="menuSection section" id="<?php echo $catName; ?>">
                <div class="titles"><h2><?php echo $category->name; ?></h2></div>
                <?php if($category->description){ ?>
                        <p class="catDescription"><?php echo $category->description; ?></p>
                <?php } ?>

                <?php $args=array(
                'orderby' => 'slug',
                'order' => 'ASC',
                'parent' => $category->term_id
                );
                $subcategories=get_categories($args);
                foreach($subcategories as $subcategory) {

                        $showPostsInSubCategory = new WP_Query();
                        $showPostsInSubCategory->query('cat='.$subcategory->term_id.'&showposts=100');
                ?>
...loop through display of subcategory

My child theme is an attempt to support 2 levels of categories. Here is the original theme's index.php.

Even though I have hard-coded which categories to include, the website only displays Posts for 1 category - Lunch.

Why is Lunch the only category being displayed?

share|improve this question
    
You don't need to have both include and exclude in your $args array -- if you read the documentation for get_categories(), include will include only the specified categories. –  Pat J Jun 17 '13 at 3:56
    
Thanks @PatJ. I've removed the exclude parameter, but the issue remains - only 1 category is displayed. –  Steve Jun 18 '13 at 0:47
    
Pastebins are discouraged in this site, you're expected to paste the code in the question. Also pasting whole files will likely not get an answer, you need to narrow the code down to the relevant parts so people can read it. Just some pointers on how to maybe get some answers. –  Wyck Jun 19 '13 at 6:30
    
try to rename the second $args to something like $args2 I think you may be over writting your loop. –  BandonRandon Jun 25 '13 at 6:32
1  
@Steve: Do you have any posts in these categories? Also remove the include part of this code - let's make it as simple as possible. Could you var_dump $categories variable just after setting it? @BandonRandom: I don't think this is the case in here. $args are only params for function - function already returned it's value... –  Krzysiek Dróżdż Jun 25 '13 at 7:29

2 Answers 2

up vote 2 down vote accepted
+50

I played with your code a little bit on my local WordPress. I think this should solve your problems.

Arguments for your first get_categories call, should look like this:

$args=array(
    'hide_empty'=>false,
    'orderby' => 'slug',
    'order' => 'ASC',
    //'include' => '5, 6, 7, 8',   - you don't need to include anything
    //'exclude' => '3, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26',   - no need to exclude anything either
    'parent' => $menuCat
);

And for the second one:

$args=array(
    'hide_empty' => false,
    'orderby' => 'slug',
    'order' => 'ASC',
    'parent' => $category->term_id
);

It works correctly on my WP.

I also would recommend to use <h3> tags to display subcategories.

share|improve this answer
    
That worked, but I don't know why, as the other categories are not empty. –  Steve Jun 26 '13 at 7:04
    
I haven't debug it to long, but maybe there is some conflict when you're using include, child_of and hide_empty=>true arguments... Or it works a little bit different than you/we assume. I'm glad my solution helped. –  Krzysiek Dróżdż Jun 26 '13 at 7:13

I duplicated the code and found that it will not work without the 'hide_empty' => false, and that it will also not work it you use the include with the child of. Once these were modified your code worked in my sandbox.

share|improve this answer

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.