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'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?
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.
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-single.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-single.php
with additional partial template for each pcat
term.
Your portfolio-single.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.
portfolio
and its categoriesportfolio_categories
, I can see all the portfolio usingarchive-portfolio.php
. And you can see your desired category items usingarchive-portfolio_category.php
as your taxonomy isportfolio_category
. – Mayeenul Islam Commented Feb 16, 2015 at 16:58register_taxonomy( 'portfolio_category', array( 'portfolio' ), $args );
and rewrite parameter can be like this:'rewrite' => array( 'slug' => 'portfolio-categories' ),
– Mayeenul Islam Commented Feb 16, 2015 at 17:09'rewrite' => array( 'slug' => 'portfolio-category' ),
, don't forget to tryarchive-portfolio-category.php
(hyphenated) too. Because I din't try this. :) – Mayeenul Islam Commented Feb 16, 2015 at 17:16