I am working on a custom theme for a network of sites that are deployed under a WordPress MultiSite installation.
Within these theme I create 4 custom post types that have two taxonomies added to all of them. The first taxonomy is the post_tag one and the other is a a custom taxonomy that resembles the category one.
I’ve chosen that all of my 4 custom post types to have their own taxonomy category because I did not want any category bleed within the post types.
Here is my code:
<?php
function o_post_types()
{
$post_types = array(
'articole' => array(
'name' => 'Articole',
'single' => 'Articol',
'icon' => 'dashicons-admin-post'
),
'evenimente' => array(
'name' => 'Evenimente',
'single' => 'Eveniment',
'icon' => 'dashicons-video-alt'
),
'concursuri' => array(
'name' => 'Concursuri',
'single' => 'Concurs',
'icon' => 'dashicons-awards'
),
'fotografii' => array(
'name' => 'Fotografii',
'single' => 'Fotografie',
'icon' => 'dashicons-format-image'
),
'promotii' => array(
'name' => 'Promoții',
'single' => 'Promoție',
'icon' => 'dashicons-share'
)
);
foreach ($post_types as $post_type) {
$icon = $post_type['icon'];
$name_lower = strtolower($post_type['name']);
$single_lower = strtolower($post_type['single']);
if ($post_type['name'] == 'Promoții') {
$name_lower = 'promotii';
$single_lower = 'promotie';
}
$labels = array(
'name' => $post_type['name'],
'singular_name' => $post_type['single'],
'add_new' => 'Adaugă ' . $single_lower,
'add_new_item' => 'Adaugă ' . $single_lower . ' nou',
'edit_item' => 'Editează ' . $single_lower,
'new_item' => $post_type['single'] . ' nou',
'all_items' => 'Listă ' . $name_lower,
'view_item' => 'Afișează ' . $single_lower,
'search_items' => 'Caută în ' . $name_lower,
'not_found' => 'Nici un ' . $single_lower . ' găsit.',
'not_found_in_trash' => 'Nici un ' . $single_lower . ' găsit în Gunoi.',
'parent_item_colon' => '',
'menu_name' => $post_type['name']
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => $name_lower, 'with_front' => false),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
'taxonomies' => array('post_tag'),
'menu_icon' => $icon
);
register_post_type("cpt_" . $single_lower, $args);
$labels = array(
'name' => 'Categorii',
'singular_name' => 'Categorie',
'search_items' => 'Caută Categorii',
'all_items' => 'Toate Categoriile',
'parent_item' => 'Categorie Părinte',
'parent_item_colon' => 'Categorie Părinte:',
'edit_item' => 'Modifică Categorie',
'update_item' => 'Actualizează Categorie',
'add_new_item' => 'Adaugă Categorie nouă',
'new_item_name' => 'Numele noii categorii',
'menu_name' => 'Categorii',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'hierarchical' => true,
'slug' => $name_lower,
'with_front' => false
),
);
register_taxonomy("tax_" . $name_lower, "cpt_" . $single_lower, $args);
}
}
EDIT 1*
The file names are as follow:
taxonomy-articole.phptaxonomy-concursuri.phptaxonomy-evenimente.phptaxonomy-fotografii.phptaxonomy-promotii.php
I am trying to display a list as a category view of all cpts that have that term. Ex: a cpt_evenimente (or cpt_events in English) will have a tax_evenimente term (or tax_events in English) like so:
Skate-park skateboard showoff cpt will have assigned a Outdoor Event term and it will have to be displayed because of its term in the taxonomy-evenimente.php loop.
The problem is that the templates for each taxonomy does not load. What is there to be done?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You have totally missed the naming convention when coming to the taxonomy archive pages, and most probably the same goes for your archive pages for your custom post types
Here is how your taxonomy archive pages should look like
-
taxonomy-{taxonomy}-{term}.php – If the taxonomy were
sometax, and taxonomy’s term weresometermWordPress would look fortaxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy ispost_formatand the terms arepost_format-{format}. i.e.taxonomy-post_format-post-format-link.php -
taxonomy-{taxonomy}.php – If the taxonomy were
sometax, WordPress would look fortaxonomy-sometax.php - taxonomy.php
- archive.php
- index.php
So, all your taxonomy templates should be called taxonomy-tax_{$name_lower}.php where $name_lower is the name assigned to the variable.
Another issue that I brought up in my comments to your posts was
Do not use hyphens in your names, make use of underscores to separate words
This is now the opportunity to test your names with and without hyphens and see how that affect how your templates are used.
Example:
If you make your taxonomy name tax-mytax and you create a template taxonomy-tax-mytax.php, you will notice that this will not work, as wordpress reads your template as follow: tax is your taxonomy name and mytax is a term
One last thing, and I don’t know if you intentionally left that out, but your function should be hooked to the init hook
EDIT 1
Just also another point on good practice, write your arguments in English and make them translatable. Someone that speaks a language different from yours will have a tough time figuring out what the labels mean, as would be the case with me :-). This is also the one big reason why your function should be hooked to init as to make the translators available
EDIT 2
It seems that your rewrite rules is causing your issue. From what was discussed in chat,
I have a taxonomy named
tax_evenimente. It is a category-like taxonomy. I have a termAlte Evenimentewith the slugalte-evenimente.site.com/evenimente/alte-evenimenteshould be displayed withtaxonomy.php.
Yes, that is what should happen with your rewrite rule, but it keeps on 404ing.
<—SECTION SCRAPPED—>
EDIT 3
I have found a great plugin to help with the rewrite rules for your custom post types. It is called Custom Post Type Permalinks (NOTE: I have no affiliation to the plugin). As your code currently stands, leave it in place
Here is how everything works:
(For the sake of examples, I’m going to use cpt_evenimente and tax_evenimente)
- Download and install the plugin
- Go to your permalinks settings page and scroll down to the settings for your custom post types. This is how it will look

-
Change
/%postname%/to/%year%/%monthnum%/%postname%/to get your desired URL - Save your permalinks
Now, for the tests
-
site.com/evenimente/alte-evenimente/Displays taxonomy archive for terms from taxonomytax_evenimenteontaxonomy-tax_evenimente.php -
site.com/evenimente/Displays the custom post type archive for custom post typecpt_evenimente -
site.com/evenimente/2014/10/testing-posts-1/Displays the single posttesting post 1
Just a note, the checkbox for “Use custom permalink of custom taxonomy archive” should be unchecked
I hope this is what you need.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0