WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. toddtwining
    Member
    Posted 1 year ago #

    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

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year 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');
    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

  3. toddtwining
    Member
    Posted 1 year 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. alchymyth
    The Sweeper & Moderator
    Posted 1 year 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
    Member
    Posted 1 year ago #

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

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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
  7. toddtwining
    Member
    Posted 1 year 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

Tags