• This is over my head. I have a theme and want to show the subcategories on the category page, but can’t figure it out for the hell of me.

    // directory home page category display
    
    function cp_directory_cat_columns($cols) {
    
        // get all cats except for the blog
    $cats = get_categories('hide_empty=0&hierarchical=0&pad_counts=1&show_count=1&orderby=name&order=ASC&exclude='.CP_BLOG_CAT_ID);
    
        // remove all sub cats from the array
    
        foreach ($cats as $key => $value){
    
            if ($value->category_parent != 0)
    
                unset($cats[$key]);
    
        }
        $i = 0;
    
        $cat_cols = $cols; // change this to add/remove columns
    
        $total_main_cats = count($cats); // total number of parent cats
    
        $cats_per_col = round($total_main_cats / $cat_cols); // items per column
    
        // loop through all the sub
    
        foreach($cats as $cat) {
            if (($i == 0) || ($i == $cats_per_col) || ($i == ($cats_per_col * 2))) {
    
            ?>
                <div class="catcol">
            <?php
            }
            ?>
          <ul>
    
                <li class="maincat"><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->name; ?></a> (<?php echo $cat->category_count; ?>)</li>
            <?php
    
            // now get all the sub cats based off the parent cat id
            $subcats = wp_list_categories('orderby=name&order=asc&hierarchical=0&show_count=1&use_desc_for_title=0&hide_empty=0&depth=1&number='.get_option('cp_dir_sub_num').'&title_li=&echo=0&child_of='.$cat->cat_ID);
            // strip out the default wp title tag since the use_desc_for_title param doesn't seem to work in WP 2.9.2
    
            $subcats = preg_replace('/title=\"(.*?)\"/','',$subcats);
            // if you want to strip out the no categories text, just uncomment the line below
    
            // $subcats = str_replace('<li>No categories</li>', '', $subcats);
            // print out all the subcats for the current parent cat
            echo $subcats;
            ?>
            </ul>
            <?php
            if (($i == ($cats_per_col - 1)) || ($i == (($cats_per_col * 2) - 1)) || ($i == ($total_main_cats - 1))) {
            ?>
                </div><!-- /catcol -->
            <?php
            }
           $i++;
       }
    }

    It is out putting the category in 3 columns. I want it to continue displaying in 3 columns but only the subcategories on the parent category page.
    Any help is welcome.
    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter geofeedz

    (@geofeedz)

    I really need help with this. Can anyone Please provide any input. PLEASE WITH CHERRY ON TOP!

    1. Install the Plugin

    We are going to do this step first because it helps to explain what we are doing. In all reality, this installation could be done at any point. The plugin we want is Idealien Category Enhancements. Click to go to the WordPress plugin page. (opens in new window)

    Why do you want this plugin ? Basically, most of what this plugin does can be done with already existent WordPress hooks and and calls. One of the unique features … and the reason we want this plugin … is that it will allow you to build a category template for a parent category and then have the same template displayed on all subcategories under that parent. You will not have to make unique templates for all of your subcategories which will save you a lot of programming time and also take up a lot less room in your files directory. Less clutter is always good ! To use this feature you will need to turn it on in the Idealien cats settings page that appears after you install the plugin.

    2. Create Category Templates

    Most WordPress Templates use the “archive.php” file to display the categories, but a more advanced template may use a “category.php” template to display the categories. To check your WordPress theme go to WordPress Admin > Appearance > Editor. Check the template files on the right hand side. If there is no “category.php” listed then your theme uses the “archives.php” file. We will need to create a “categories.php” file.

    A little back information for everyone here. WordPress uses a hierarchy structure to determine what template file to use when displaying categories. The hierarchy is as follows:
    1. category-2.php – This template will be used to display the category that has the category ID #2.
    2. category.php – If the template in #1 does not exist for a certain category, then this template is used.
    3. archive.php – Used if no category templates exist.
    4. index.php – Used in no archive template exists and no category template.

    OK … so you can see where we are going with this. We will want to create category templates that are specific to your parent categories. For our example lets say you have two parent categories … Stories (cat ID #2) and Photos (Cat ID#4) … and each one has 25 different sub-categories. All together you have 52 categories / subcategories, but we only need to make two (2) category templates because the subcategories will inherit their templates from the parents. (nothing like a little WP DNA)

    If your theme has an archive.php file and no category.php file then make an exact copy (with copy and paste) of the archive file and save it as “category.php”. (do not change or delete your archive file) This will be our foundation … our starting point. Now your blog category display is using the category file and not the archive file, but your site still has the exact same look.

    Now let’s make the template for the Photos Category (ID #4). Make an exact copy of the category file created above and save it as “category-4.php”. Make sure that the number used in your file name exactly matches the cat ID number of the category you would like to use this template on. At the very top of the category-4.php page paste the following code:

    <?php /*
    Category Template: YOUR-CATEGORY-TEMPLATE-NAME
    */ ?>

    For our example, you would want to use something like ” Category Template: Photos Template “. This will give your template a name that can be used in the template selection screen within WP admin.

    Back in your WP Admin screen go to Admin > Posts > Categories. For our example, click “edit” for the photos category and select the photos template as the template for this category. You do not need to select templates for all of the subcategories that are under the parent category as the plugin will do this for us automatically. You also do not need to create templates for all of the subcategories unless you would like to.

    Now you may modify the category-4.php template any way you would like in order for it to better represent your photos. You can change colors, backgrounds, layout, sidebars …whatever you would like … or you can leave it just the way it is and move to step #3.

    Repeat this for all of your parent / top level categories.

    3. Place the Sub-Category Call

    This is simple. We are just going to use the WordPress ” list categories ” template tag. Using the photos category example from above, place the following code in the category-4.php template wherever you would like to display your subcategories:

    <?php wp_list_categories(’child_of=4‘); ?>

    Make sure your “child_of” number in the code is the same as your PARENT category ID. Basically it should be the same number that you used in the NAME of the category template file. In the above example, all 25 subcategories of the “photos” category will be displayed. Since we are using the Idealien plugin, the subcategory list will also be displayed on all 25 of the subcategories.

    There are other design and display arguments that you can pass with the wp_list_categories tag. You can see the list here.

    And we are now done. I may have got a little long-winded above … this is actually a rather easy procedure. Sit back, have a cool beverage, and know that you just gave your site a lot better navigation. I think your visitors will thank you for this. Remember, you can do a lot of different modifications … many more then I mentioned above.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sub categories on category page’ is closed to new replies.