[resolved] Click on category goes to list of child categories (7 posts)

  1. toddtwining
    Posted 2 years ago #

    This is a simple thing in CMS platforms but with WP I can't get it to happen. My client will be adding alot of categories and sub-categories, and needs to have it when a user to clicks on a category link, they are brought to a page with a list of the child categories. This will allow a simple "drill down" for users. also, There are 3-4 child levels for each category. I cannot create php staements for each as that solution would take forever I would think I could use the structure within WP already to simply accomplish this, but I have not found any solutions.
    Any Help is appreciated

  2. Michael
    Part-Time Forum Moderator
    Posted 2 years ago #

    if a user clicks on a category, that leads him to a category archive page, done with category.php

    right at the start of that template, you can check if the current catgory has a parent or children, and dependant on that, you can show the list of sub-categories or posts.

    this might be useful:
    $the_active_cat_ID = get_query_var('cat');

  3. toddtwining
    Posted 2 years ago #

    Thanks for the prompt reply. I am not much of a coder so I am finding the dots a little too far apart to connect. It seems I need to add code to category.php
    I see the examples of arguments, but I am not sure how to use the code to get my sub category names to display in place of sub cat posts.
    I appreciate any further "dumbed down" guidance.
    Thanks again.

  4. Michael
    Part-Time Forum Moderator
    Posted 2 years ago #

    what theme are you using right now?

    to allow the suggestions to match your existing structure, please paste the full code of category.php into a pastebin and post the link to it here - ( http://codex.wordpress.org/Forum_Welcome#Posting_Code )

  5. toddtwining
    Posted 2 years ago #

    category.php is-

  6. Michael
    Part-Time Forum Moderator
    Posted 2 years ago #


    this section is new:

    //show a list of direct child (sub) categories if available//
    	if( $list = wp_list_categories('echo=0&title_li=&show_option_none=&depth=1&child_of='.get_query_var('cat')) ) {
    		echo '<h3 class="sub-categories-title">Sub Categories List</h3>';
    		echo '<ul class="sub-categories">' . $list . '</ul>';
    //tweak the query to show only posts direct in the current category//
    	global $wp_query;
    	$args = array_merge( $wp_query->query_vars, array( 'category__in' => array(get_query_var('cat')) ) );
    	query_posts( $args );

    the first part checks if there are direct children (sub-categories) of the current category, then output the list.

    the second part changes the query to show only posts which are in the direct category (normally the category archive will also show posts which are in sub categories of the current category).

    new introduced css classes for better formatting:

  7. toddtwining
    Posted 2 years ago #

    Boy did that work perfect!
    Thank you so much for awsome support!

Topic Closed

This topic has been closed to new replies.

About this Topic