Support » Developing with WordPress » Can a category page template be applied to a static page?

  • Resolved eadwig

    (@eadwig)


    Hello everyone

    I would like to apply a category page template (category.php) to each static page which is directly linked from a menu. I have a reasonably good understanding of the template hierarchy, but cannot work out how the category.php file can be used to display the static pages directly associated with the menu items’ links.

    I am aware that making a duplicate of category.php and renaming it page.php does it but I would rather use as few template files as possible.

    If someone out there know how category.php can be used for the static pages, kindly share the how-to. Google searches have returned nothing helpful.

Viewing 8 replies - 1 through 8 (of 8 total)
  • There is a filter for template_redirect that you can use, but the question is Why?
    The category template expects the query to have been for a category, so things like the page title would be displayed by calling the function for a single category title, but the Page does not even have a category since it’s a different post type.
    The template file does not do the query; WordPress does that and decides based on the query which template file to use. It’s best if they match.

    What problem are you actually trying to solve?

    Thread Starter eadwig

    (@eadwig)

    Thank you for the reply.

    I read the page concerning the template_redirect filter which looks useful. What I aim to achieve is to display the same list of posts belonging to a specific category upon clicking a menu link and clicking an archive like, the both of which relate to the same category (e.g. clicking the ‘NEWS’ link in the top bar and clicking the archive link for the NEWS section on the front page).

    Is it more resource-intensive to make WordPress do the query using page.php? I would like to know if there is a more effective way of achieving the same outcome.

    The problem with trying to show posts on a Page address is the pagination. You can split Pages and Posts into multiple “pages” using the <!--nextpage--> tag and the URL looks the same as when an archive page has multiple “pages”.
    WordPress has its internal rewrite rules that it uses to determine what to retrieve from the database for each address. If you go to a Page address, WP will retrieve the content for that single Page and all the variables(ID, type, content, title, taxonomy, etc.) are set for that single Page. Going to an archive page address, WP will retrieve the content for X number of Posts and all the variables are set for that group of Posts. The theme template files don’t retrieve the data. The template file is chosen by WP based on the data retrieved.

    It seems to me that you could change the link in your menu to be the archive page instead of the single Page.

    Thread Starter eadwig

    (@eadwig)

    Thank you for the further advice. I embedded a template_redirect function in functions.php and it is now working.

    In case someone else needs to implement a similar configuration, here is the code embedded in functions.php:

    function my_page_template_redirect(){
    if(is_page(‘news’)){
    wp_redirect(home_url(‘/category/news’));
    die;
    }
    }
    add_action(‘template_redirect’, ‘my_page_template_redirect’);
    do_action(‘template_redirect’);

    • This reply was modified 4 years, 10 months ago by eadwig.
    • This reply was modified 4 years, 10 months ago by eadwig.

    It would have been a lot easier to just change your menu link. All you did was load the normal category archive page when they click on the Page link that you had.

    Hopefully, no one will copy that code. It creates a new action and calls it at the wrong time and then redirects. Crazy.
    (the template_redirect I mentioned is a filter, not an action)

    Thread Starter eadwig

    (@eadwig)

    Replacing add_action() with add_filter() is working:

    function my_page_template_redirect(){
    if(is_page(‘news’)){
    wp_redirect(home_url(‘/category/news’));
    die;
    }elseif(is_page(‘events’)){
    wp_redirect(home_url(‘/category/events’));
    die;
    }elseif(is_page(‘brands’)){
    wp_redirect(home_url(‘/category/brands’));
    die;
    }
    }
    add_filter(‘template_redirect’, ‘my_page_template_redirect’);

    Thread Starter eadwig

    (@eadwig)

    The example given in WP Codex for some reason calls add_action() instead of add_filter(), even though it introduces tempalte_redirect() as a hook:

    https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect

    Not sure why the example is written that way…

    I apologize for misleading you with my wrong information. template_redirect is an action, as you had it before. In my head it was confused with template_include which is a filter.

    But what is the point of your code? Can’t you just put the category page address into your menu? And delete the Page that is never shown? There is an ancient plugin that removed the word ‘category’ from the URL. I’m sure there are still plugins that do this.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Can a category page template be applied to a static page?’ is closed to new replies.