1

I am currently trying to do a directory style site, and I currently have parent categories and child categories.

Right now, I have the parent categories showing and when a parent category link is clicked I'm using category.php to show the list of child categories.

Now I want to that when a child category link is clicked WordPress shows all the posts in this child category.

Is there a conditional statement or a way that I can have the child categories go to the archive.php so it can display something else?

Right now parent and the child categories pull from the category.php, so they end up being the same page.

0

You can do this couple of ways.

  1. By checking depth of the category/sub-category. If we are browsing main/parent categories then show a custom template, if we are on sub-categories then show another template and this goes on.
  2. We can check if a category is ending category of not. Ending category means if a category does not have any child then it should show default category.php template.

Which method you should use is depends on your project requirements. Here I am going to explain first one because it's very flexible and I think will suit your purpose.

WordPress have template_include function that executes immediately before WordPress includes the predetermined template file. This can be used to override WordPress's default template behavior.

function wpse_template_check( $template ) {
    if ( is_category() ) {
        // Get category information
        $cat = get_query_var( 'cat' );
        $cats_str = get_category_parents( $cat, false, '%#%' );
        $cats_array = explode('%#%', $cats_str);
        $cat_depth = sizeof( $cats_array )-2;

        // Check category depth
        $new_template = locate_template( array( 'category-custom-template.php' ) );
        if ( $cat_depth == 0 && '' != $new_template ) {
            return $new_template;
        }
    }
    return $template;
}
add_filter( 'template_include', 'wpse_template_check' );

This function calculates category depth and checks if current category is main/parent categories. If it's true and category template exists then it will replace category template to category-custom-template.php.

I have tested it and it's working but you should note that it's based on category depth so all parent categories will have same custom template.

Although you can customize this function to do more and as fit your requirements.

0

I think I get what you mean. By default, when a category is clicked and you are directed to the category page, posts from the current category and all its child categories are displayed

This is how category pages are constructed by default. The main query (which also uses WP_Query) uses a tax_query to call posts from the db on category and taxonomy archive pages. A tax_query on category pages and taxonomy pages are exactly the same as there are virtually no difference between the build in taxonomy category and custom taxonomies. See this post for an explanation .

By default, if you look at a tax_query, the include_children parameter is set to true, which means that the child terms of the current term being queried are also included. So, you basically just need to set this parameter to false. To accomplish this, you will need to make use of the parse_tax_query filter

You can simply do something like this in your functions.php file (Requires PHP 5.3+)

add_filter( 'parse_tax_query', function ( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_category() ) {

        $query->tax_query->queries[0]['include_children'] = 0;

    }
}

This will ensure that when a category is clicked, it will only show posts from that particular category, and not also from its children

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.