4

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.php
  • taxonomy-concursuri.php
  • taxonomy-evenimente.php
  • taxonomy-fotografii.php
  • taxonomy-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?

  • what are your taxonomy template filenames? are you trying to load a taxonomy term archive? – Milo Oct 10 '14 at 17:54
  • Do not use hyphens in your names, make use of underscores to separate words – Pieter Goosen Oct 11 '14 at 7:48
  • @Milo I have edited my question. I am trying to display all cpts that belong to all terms found within a certain taxonomy for now. – aifrim Oct 13 '14 at 18:02
  • @PieterGoosen: No more hyphens, only underscores. Same problem. – aifrim Oct 13 '14 at 18:02
  • if the taxonomy is tax_evenimente, the template should be taxonomy-tax_evenimente.php. – Milo Oct 13 '14 at 18:10
5
+100

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 were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is post_format and the terms are post_format-{format}. i.e. taxonomy-post_format-post-format-link.php

  • taxonomy-{taxonomy}.php - If the taxonomy were sometax, WordPress would look for taxonomy-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 term Alte Evenimente with the slug alte-evenimente. site.com/evenimente/alte-evenimente should be displayed with taxonomy.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

enter image description here

  • 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 taxonomy tax_evenimente on taxonomy-tax_evenimente.php

  • site.com/evenimente/ Displays the custom post type archive for custom post type cpt_evenimente

  • site.com/evenimente/2014/10/testing-posts-1/ Displays the single post testing 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.

  • Let us continue this discussion in chat. – aifrim Oct 14 '14 at 15:10
  • Please see my update. Your taxonomy pages should now work – Pieter Goosen Oct 14 '14 at 16:50
  • I have made a test and it still gives conflict. I need to create a specific permastruct rewrite rule. I will do some further tests on this conflict and hopefully write a wiki about it. – aifrim Oct 15 '14 at 15:21
  • On my side if I enter site.com/evenimente/alte-evenimente it works and taxonomy-tax_evenimente.php is used. Don't know if this will help – Pieter Goosen Oct 15 '14 at 15:28
  • Did you have a cpt with the same slug? – aifrim Oct 15 '14 at 15:32

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.