admin管理员组

文章数量:1291426

I have created a custom post type (products), and at the moment I have 2 custom taxonomies (lips and eyes).

I can get the archive to show up for 'products', and I can get to the URL for the archive for the categories under 'lips', but I cannot get the archive to show for the taxonomies.

I have been through every question I can find, and I have checked the answers against what I have done, but for the life of me I can't figure out what is going wrong.

I have a taxonomy template, 'taxonomy-lips.php', but it just keeps defaulting back to my front-page.php template.

Here is my code for the post type:

function my_first_post_type() {

$args = array(
    
    'labels' => array(
        
        'name' => 'Products',
        'singular_name' => 'Product',
        
        ),
        
    'hierarchical' => true,
    'public' => true,
    'has_archive' => true,
    'menu_icon' => 'dashicons-list-view',
    'supports' => array('title', 'editor', 'thumbnail'),
    'rewrite' => array('slug' => 'shop-products'),
    'taxonomies' => array('lips', 'eyes'),
    
    ); 
    register_post_type('products', $args);
    }
    add_action('init', 'my_first_post_type');

And here is the code for my taxonomy:

    function my_first_taxonomy()
    {
    $args = array(
    
    'labels' => array(
        
        'name' => 'Lips',
        'singular_name' => 'Lips',
        ),
        
    'public' => true, 
    'hierarchical' => true,
    'rewrite' => array('slug' => 'lips'),
   
    
    );
    
    register_taxonomy('lips', array('products'), $args);

    }
    add_action('init', 'my_first_taxonomy');

I have reset the permalinks many times, and nothing changes.

Sorry, I know this question has been asked many times, I just can't figure out what I have done! I'm very new to all of this, so be gentle lol, I might not understand anything too complex!

I have created a custom post type (products), and at the moment I have 2 custom taxonomies (lips and eyes).

I can get the archive to show up for 'products', and I can get to the URL for the archive for the categories under 'lips', but I cannot get the archive to show for the taxonomies.

I have been through every question I can find, and I have checked the answers against what I have done, but for the life of me I can't figure out what is going wrong.

I have a taxonomy template, 'taxonomy-lips.php', but it just keeps defaulting back to my front-page.php template.

Here is my code for the post type:

function my_first_post_type() {

$args = array(
    
    'labels' => array(
        
        'name' => 'Products',
        'singular_name' => 'Product',
        
        ),
        
    'hierarchical' => true,
    'public' => true,
    'has_archive' => true,
    'menu_icon' => 'dashicons-list-view',
    'supports' => array('title', 'editor', 'thumbnail'),
    'rewrite' => array('slug' => 'shop-products'),
    'taxonomies' => array('lips', 'eyes'),
    
    ); 
    register_post_type('products', $args);
    }
    add_action('init', 'my_first_post_type');

And here is the code for my taxonomy:

    function my_first_taxonomy()
    {
    $args = array(
    
    'labels' => array(
        
        'name' => 'Lips',
        'singular_name' => 'Lips',
        ),
        
    'public' => true, 
    'hierarchical' => true,
    'rewrite' => array('slug' => 'lips'),
   
    
    );
    
    register_taxonomy('lips', array('products'), $args);

    }
    add_action('init', 'my_first_taxonomy');

I have reset the permalinks many times, and nothing changes.

Sorry, I know this question has been asked many times, I just can't figure out what I have done! I'm very new to all of this, so be gentle lol, I might not understand anything too complex!

Share Improve this question edited Jun 10, 2021 at 1:30 Siobhan M asked Jun 9, 2021 at 22:52 Siobhan MSiobhan M 112 bronze badges 7
  • "it just keeps defaulting back to my front-page.php template" - how did you know it's not actually taxonomy-lips.php? What is the URL of the page, is it like example/lips/category-slug? – Sally CJ Commented Jun 10, 2021 at 0:37
  • Also, in the question, the register_post_type() is outside the my_first_post_type() function.. – Sally CJ Commented Jun 10, 2021 at 0:43
  • 1 Yes, the URL I am using is as you put above. example/lips/category-slug. I can get to that one. I can't get to example/lips. – Siobhan M Commented Jun 10, 2021 at 1:15
  • Hmmm, ok. I'll adjust that, although that doesn't seem to have had an effect on the post type. I can use that without issue. – Siobhan M Commented Jun 10, 2021 at 1:16
  • Sorry that probably didn't answer your question properly. When I go to example/lips, the page doesn't show an empty screen as my template has no coding in it. It shows my front-page.php layout and coding. It did this initially for the post type page too, but after I reset the permalinks that part started working correctly. Not this time :( – Siobhan M Commented Jun 10, 2021 at 1:29
 |  Show 2 more comments

1 Answer 1

Reset to default 2

I cannot get the archive to show for the taxonomies

And in the comments you said:

I can't get to example/lips

And similar to what I said here:

  • Taxonomies, unlike post types, do not have an archive page (which displays posts from all terms in the specific taxonomy), so it's normal if you "can't get to" example/lips or example/eyes, i.e. example/<taxonomy key>.

  • And secondly, taxonomy templates like taxonomy-lips.php actually rely upon the current taxonomy term, i.e. there has to be a term being queried in order for a taxonomy template to be used, and for functions like is_tax() to return true. So remember, the query determines the template and not the other way round. And for example example/lips/category-slug worked (i.e. taxonomy-lips.php was used) because there's a term being queried which is the one with the slug category-slug.

So no matter how many times you flush/regenerate the rewrite rules, taxonomies simply do not have an "all-terms" archive, only specific terms in the taxonomies. See @bosco's answer here if you're interested in knowing more tech stuff about taxonomy's "archive page", which by default do not exist or not implemented in WordPress core.

I hope this answered your question, and if all you wanted to have is for the example/lips and example/eyes to display posts from all/any terms in the lips/eyes taxonomy, then an easy way is:

  1. Create a custom Page (post of type page) and give it the lips or eyes slug.

  2. Assign a custom page template to that Page.

  3. And then make a secondary/custom WP_Query query in that very template.

本文标签: Custom Taxonomy template is not recognised