Custom Post/Taxonomy term archive
-
Hi,
I created a custom post type:register_post_type('news', array( 'has_archive' => true, 'rewrite' => array('slug' => 'news'), 'supports' => array('title', 'editor', 'excerpt', 'thumbnail'), 'public' => true, 'show_in_rest' => true, 'labels' => array( 'name' => 'News', 'add_new_item' => 'Add News', 'edit_item' => 'Edit News', 'all_items' => 'All News', 'singular_name' => 'News' ), 'menu_icon' => 'dashicons-calendar-alt' ));
and a custom taxonomy:
function news_taxonomy() { $labels = array( 'name' => _x( 'News Category', 'Taxonomy General Name' ), 'singular_name' => _x( 'News Category', 'Taxonomy Singular Name' ), 'search_items' => __( 'Search News Category' ), 'all_items' => __( 'All News Categories' ), 'parent_item' => __( 'Parent News Category' ), 'parent_item_colon' => __( 'Parent News Category:' ), 'edit_item' => __( 'Edit News Category' ), 'update_item' => __( 'Update News Category' ), 'add_new_item' => __( 'Add News Category' ), 'new_item_name' => __( 'New News Category' ), 'menu_name' => __( 'News Category' ), ); $args = array( 'hierarchical' => true, // like categories or tags 'labels' => $labels, 'show_in_rest' => true, 'show_ui' => true, // add the default UI to this taxonomy 'show_admin_column'=> true, // add the taxonomies to the wordpress admin 'query_var' => true, 'rewrite' => array( 'slug' => 'news'), ); register_taxonomy( 'news_tax', 'news', $args ); }
Then created an archive-news.template where a navigation menu with news categories is displayed (in the wp dashboard I created a few dummy news categories (news_a, news_b, news_c and assigned them to a few news posts that I created. The archive-news template display them correctly and my nav menu correctly shows the news categories:
<nav id="nav_events"> <div id="navmenu" class="events"> <ul class="pagination"> <li class="page-item active"><a class="page-link current" href="<?php echo get_post_type_archive_link( 'news' ); ?>">All</a></li><?php $terms = get_terms(array( 'taxonomy' => 'news_tax' )); foreach($terms as $term) { echo '<li class="page-item"><a class="page-link" href="'.get_term_link($term).'">'.$term->name.'</a></li>'; } ?> </ul> </div> </nav>
When I click on any of the news categories/terms in the nav/menu (eg. news_a, news_b or c), I get 404.
I’ve created taxonomy-news_tax.php and taxonomy-news_tax-news_a.php but it does not seem to be picking them up. While setting up the news custom post/taxonomy I mirrored whatever I did on the ‘event’ custom post/taxonomy, which works fine on the same wordpress installation.
Please advise. Thank you
- The topic ‘Custom Post/Taxonomy term archive’ is closed to new replies.