WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Click on category goes to list of child categories

[Resolved] Click on category goes to list of child categories

  • Hi,
    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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Michael

    @alchymyth

    Forum Moderator

    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');
    http://codex.wordpress.org/Function_Reference/get_category
    http://codex.wordpress.org/Function_Reference/get_categories
    http://codex.wordpress.org/Template_Tags/wp_list_categories

    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.

    Michael

    @alchymyth

    Forum Moderator

    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 )

    Theme=Coraline
    category.php is-
    http://pastebin.com/6wkysMVd

    Michael

    @alchymyth

    Forum Moderator

    http://pastebin.com/R2fvgrHx

    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:

    .sub-categories
    .sub-categories-title

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Click on category goes to list of child categories’ is closed to new replies.
Skip to toolbar