0

I have a client with a lumberyard who needs Lumber to be a category with subcategories. We would like to use category-lumber.php for both the parent category and the subcategories. I found this post from 2010 that addresses the same question, but was not able to make it work: Make all subcategories use the template of its category parent? It could be that I'm using it wrong, or that the functions are totally out of date.

You can see the goal on https://goosebaylumber.net/category/lumber. Currently the subcategories are picking up the default archive.php

Update: now that the issue is fixed, the link given is no longer showing the default layout. But, it's the same layout as the blog, which is using archive.php.

2

The main takeaway from that code you linked to is using the category_template filter to modify the template that WordPress will use when a user is viewing a child category of the lumber category. I put this together for you as it's a bit simpler when you're only working with the one template:

// add this filter to your themes functions.php file
add_filter('category_template', 'yourprefix_use_lumber_category_template_for_child_categories');

function yourprefix_use_lumber_category_template_for_child_categories( $template ) {

    // get the current category object
    $category = get_queried_object();

    // if current category has lumber category(assuming lumber category id == 7) as its parent...
    if( $category->parent == 7 ) {

        // ...then find the named category-lumber.php template in theme, and replace whatever default $template is found by WordPress...
        $template = locate_template('category-lumber.php');

        // ...finally return the lumber category template file
        return $template;

    }
    else {

    // ...the current category has no lumber category as parent, so just return default $template file found by WordPress
    return $template;

    }

}
  • 1
    As a general understanding clarification, what structure is a 'filter'? Is it a function or a variable? In the documentation I've read, this point has not been clear for me. – Nora McDougall-Collins Jan 23 '18 at 15:49
  • This code did the job! As an additional aside, the posts in this category and subcategories are from custom post types. One of the confusions in thinking through the problem was whether to use a template based on the category or a template based on the custom post type. The answer to meet the site owner's goals was to use category based templates. – Nora McDougall-Collins Jan 23 '18 at 17:17
  • @NoraMcDougall-Collins Glad you got it working! A WordPress “filter” is a type of hook that allows you to modify data(like the $template variable in your case) that is internal to WordPress. There’s also “action” hooks that allow you to run your own functionality at certain points of the WordPress loading process. Here’s a link from the WordPress Plugin handbook that describes filter and action hooks fairly well: developer.wordpress.org/plugins/hooks – Ben HartLenn Jan 23 '18 at 19:22

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.