2

I've created a custom post type portfolio and a taxonomy of course.

function taxonomies_portfolio() {
    $labels = array(
        'name'              => _x( 'Portfolio categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Portfolio categories', 'taxonomy singular name' ),
        'search_items'      => __( 'Query portfolio categories' ),
        'all_items'         => __( 'All portfolio categories' ),
        'parent_item'       => __( 'Parent category' ),
        'parent_item_colon' => __( 'Parent category:' ),
        'edit_item'         => __( 'Edit portfolio category' ), 
        'update_item'       => __( 'Update portfolio category' ),
        'add_new_item'      => __( 'Add Edit portfolio category' ),
        'new_item_name'     => __( 'New portfolio category' ),
        'menu_name'         => __( 'Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'rewrite' => true
    );
    register_taxonomy( 'portfolio_category', 'portfolio', $args );
}
add_action( 'init', 'taxonomies_portfolio', 0 );

Is it possible to create 1 template file to display all items of a single category? I've tried to create a taxonomy.php but without success. What is the correct template name to work with?

  • I made a CPT portfolio and its categories portfolio_categories, I can see all the portfolio using archive-portfolio.php. And you can see your desired category items using archive-portfolio_category.php as your taxonomy is portfolio_category. – Mayeenul Islam Feb 16 '15 at 16:58
  • @MayeenulIslam Thanks for your response. I've created a "archive-portfolio_category.php" and a "taxonomy-portfolio_category.php" without success. My WhatTheFile plugin still returns: archive.php – p2or Feb 16 '15 at 17:06
  • Register taxonomy function should be like this: register_taxonomy( 'portfolio_category', array( 'portfolio' ), $args ); and rewrite parameter can be like this: 'rewrite' => array( 'slug' => 'portfolio-categories' ), – Mayeenul Islam Feb 16 '15 at 17:09
  • Sorry, in your case rewrite will be: 'rewrite' => array( 'slug' => 'portfolio-category' ),, don't forget to try archive-portfolio-category.php (hyphenated) too. Because I din't try this. :) – Mayeenul Islam Feb 16 '15 at 17:16
  • 1
3

According to the Wordpress Codex page on Template Hierarchy, you create a template file with the name of taxonomy-portfolio_category.php. WordPress will use that to display the archive for that taxonomy. You can also use taxonomy-portfolio_category-{term_name}.php to create templates for specific terms in your taxonomy.

| improve this answer | |
  • Thanks Manny, but is it possible to use 1 template for all categories instead of creating 3 templates for 3 categories? – p2or Feb 16 '15 at 16:58
  • Yeah, the first part of my answer : taxonomy-portfolio_category.php – Manny Fleurmond Feb 16 '15 at 17:08
  • Ok, thanks Manny - but unfortunately it didn't work, is there still something to consider? – p2or Feb 16 '15 at 17:13
  • Whats not working? How are you testing it? – Manny Fleurmond Feb 16 '15 at 17:20
  • 1
    For your taxonomy, it should be portfolio_category/books – Manny Fleurmond Feb 16 '15 at 17:26
0

You do not have to use the WordPress standard tempates to manage the several taxonomies' templates for the same custom post type.

Assume you have your: 1) portfolio CPT with the 1) pcat taxonomy and 3) 'sites', 'apps' and 'design' terms created in this taxonomy (here slugs are shown).

Case 1: You may want to show the same template for any of these pcat taxonomies. Just use the same single portfolio-signle.php template with the code that shows any single portfolio record in a uniform way.

Case 2: Now say you want to show different template for each portfolio CPT record depending on the pcat taxonomy term ('sites', 'apps', 'design', 'whatever') assigned to that record.

You can do this still using the same portfolio-signle.php with additional partial template for each pcat term.

Your portfolio-signle.php has to hold this code:

<?php
get_header();
// Here you get the particular record of the `portfolio` CPT.
global $post;
// Get the array of 'pcat' taxonomy terms attached to the record
// and take the slug of first term only (just for brevity)
$txslug = get_the_terms($post, 'pcat')[0]->slug;
// Dynamically prepare the file name
$filename = get_template_directory() . '/partials/_portfolio-single-'.$txslug.'.php';
// Check if the file exists & readable
if (is_readable($filename)) {
    // The case when you  created the sub-template partial for the particular `pcat` term.
    include get_template_directory() . '/partials/_portfolio-single-'.$txslug.'.php';
} else {
    // The case for all other `pcat` taxonomy terms.
    include get_template_directory() . '/partials/_portfolio-single-other.php';
}
get_footer();

As you see from the code above you will have to create the respective partial sub-templates for each pcat taxonomy term you assign to your posts that will actually handle the look of the taxonomy term.

Or / and create the /partials/portfolio-single-other.php to handle all the terms that you want to look uniform way.

This will keep your theme files well-organized and for at no code cost allow you flexibly manage the look of the different taxonomy terms'.

NB: Do not forget to re-declare global $post; at the top of your '/partials/_portfolio-single-'.$txslug.'.php' templates. You will get the access to the CPT object you want to show at no additional costs.

| improve this answer | |

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.