• Resolved portia79

    (@portia79)


    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @portia79 , you forgot to add public in news_taxonomy(), add the public into $args, it should look like this

      $args = array(
        'hierarchical'      => true,     // like categories or tags
        'labels'            => $labels,
        'public'            => true,
        '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'),
      );

    let me know if you still have this issue.

    Thread Starter portia79

    (@portia79)

    Hi,

    Thank you, however, that has not solved the problem. ??

    Am really puzzled why it does not pick up taxonomy-news_tax.php

    I just re-read your code again. and there’s something problem here, the post type of slug same as the taxonomy slug.

    in register post type, you rewrite the slug to 'rewrite' => array('slug' => 'news'),, try to change the slug (rewrite) of the taxonomy to news-test.

    then don’t forget to refresh it, here the link:
    https://codex.www.ads-software.com/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation

    Thread Starter portia79

    (@portia79)

    Thank you. That was it!!!

    Cool @portia79 , I hope you change the status to resolved, so other know how to solve it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Post/Taxonomy term archive’ is closed to new replies.