categories - Custom post type taxonomy template

admin2025-05-31  0

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?

Share Improve this question edited Feb 16, 2015 at 17:22 p2or asked Feb 16, 2015 at 16:46 p2orp2or 2271 gold badge4 silver badges15 bronze badges 6
  • 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 Commented Feb 16, 2015 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 Commented Feb 16, 2015 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 Commented Feb 16, 2015 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 Commented Feb 16, 2015 at 17:16
  • 1 smashingmagazine/2014/08/27/… – Mayeenul Islam Commented Feb 16, 2015 at 17:23
 |  Show 1 more comment

2 Answers 2

Reset to default 5

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748633916a313674.html

最新回复(0)