0

I've created custom post type and it's taxonomies. The code is:

add_action( 'init', 'register_cpt_portfolio' );

function register_cpt_portfolio() {

    $labels = array( 
        'name' => _x( 'portfolios', 'portfolio' ),
        'singular_name' => _x( 'portfolio', 'portfolio' ),
        'add_new' => _x( 'Add New portfolio', 'portfolio' ),
        'add_new_item' => _x( 'Add New portfolio', 'portfolio' ),
        'edit_item' => _x( 'Edit portfolio', 'portfolio' ),
        'new_item' => _x( 'New portfolio', 'portfolio' ),
        'view_item' => _x( 'View portfolio', 'portfolio' ),
        'search_items' => _x( 'Search portfolios', 'portfolio' ),
        'not_found' => _x( 'No portfolios found', 'portfolio' ),
        'not_found_in_trash' => _x( 'No portfolios found in Trash', 'portfolio' ),
        'parent_item_colon' => _x( 'Parent portfolio:', 'portfolio' ),
        'menu_name' => _x( 'portfolios', 'portfolio' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'Test Portfolio Posts',
        'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'page-attributes' ),
        'taxonomies' => array( 'category' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 2,

        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'page'
    );

    register_post_type( 'portfolio', $args );
}

add_action( 'init', 'register_taxonomy_portfolio_taxonomies' );

function register_taxonomy_portfolio_taxonomies() {

    $labels = array( 
        'name' => _x( 'portfolio_taxonomies', 'portfolio_taxonomies' ),
        'singular_name' => _x( 'portfolio_taxonomy', 'portfolio_taxonomies' ),
        'search_items' => _x( 'Search portfolio_taxonomies', 'portfolio_taxonomies' ),
        'popular_items' => _x( 'Popular portfolio_taxonomies', 'portfolio_taxonomies' ),
        'all_items' => _x( 'All portfolio_taxonomies', 'portfolio_taxonomies' ),
        'parent_item' => _x( 'Parent portfolio_taxonomy', 'portfolio_taxonomies' ),
        'parent_item_colon' => _x( 'Parent portfolio_taxonomy:', 'portfolio_taxonomies' ),
        'edit_item' => _x( 'Edit portfolio_taxonomy', 'portfolio_taxonomies' ),
        'update_item' => _x( 'Update portfolio_taxonomy', 'portfolio_taxonomies' ),
        'add_new_item' => _x( 'Add New portfolio_taxonomy', 'portfolio_taxonomies' ),
        'new_item_name' => _x( 'New portfolio_taxonomy', 'portfolio_taxonomies' ),
        'separate_items_with_commas' => _x( 'Separate portfolio_taxonomies with commas', 'portfolio_taxonomies' ),
        'add_or_remove_items' => _x( 'Add or remove portfolio_taxonomies', 'portfolio_taxonomies' ),
        'choose_from_most_used' => _x( 'Choose from the most used portfolio_taxonomies', 'portfolio_taxonomies' ),
        'menu_name' => _x( 'portfolio_taxonomies', 'portfolio_taxonomies' ),
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'show_admin_column' => true,
        'hierarchical' => true,

        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'portfolio_taxonomies', array('portfolio'), $args );
}

Now I see this in the custom post type admin panel

enter image description here

That's fine and works well to create custom post type and taxonomy.

Also I've created two taxonomies.

enter image description here

Now I want to show the portfolios in two templates according to the taxonomy.

  • if the custom post type's taxonomy is taxtemp1 then it should be displayed in 1st taxonomy template at the front end.

  • if the custom post type's taxonomy is taxtemp2 then it should be displayed in 2nd taxonomy template at the front end.

for that I've created two php files.

  1. taxonomy-portfolio_taxonomies-taxtemp1.php
  2. taxonomy-portfolio_taxonomies-taxtemp2.php

I thought it should work but when I visit to the page it shows / it is displayed in the default page template. I am using twenty twelve theme.

enter image description here

enter image description here

Why it's NOT displayed on the proper templates I've created (taxonomy-portfolio_taxonomies-taxtemp1.php and taxonomy-portfolio_taxonomies-taxtemp2.php ) ?

But archive page works fine for this custom post type: http://localhost/wp-test/portfolio/

0

I used:

$args = array(
    ...
    'rewrite' => array( 'slug' => 'portfolio-taxonomies' ),
    ...
);

register_taxonomy( 'portfolio_taxonomies', array('portfolio'), $args );

So my URL is predictable.

And you did one thing wrong, URL you are putting is NOT correct. If the rewrite rule is implemented, then they would be:

http://localhost/wp-test/portfolio-taxonomies/taxtemp1/
http://localhost/wp-test/portfolio-taxonomies/taxtemp2/

To grab the correct URL, get into:

/wp-admin/edit-tags.php?taxonomy=portfolio_taxonomies&post_type=portfolio

And hover on each Taxonomy term and click on the "View" link to trigger its respective URL. If the template is till not triggering, then we have to think further.

  • thanks for that.. I've changed my rewrite part. But still http://localhost/wp-test/portfolio-taxonomies/taxtemp1/ goes to 404 page. Also http://localhost/wp-test/portfolio-taxonomies/taxtemp1/ is it's archive but not the page.. right?? http://localhost/wp-test/portfolio/taxonomy-template-2/ is a custom post type page. I am trying to set a template for this page... – Riffaz Starr Mar 28 '15 at 13:05

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.