$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>permalinks - Why does page for hierarchical taxonomy parent term go 404?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

permalinks - Why does page for hierarchical taxonomy parent term go 404?

matteradmin9PV0评论

I have a custom taxonomy, "type", which is 'hierarchical' => true,.

Terms include:

  • "Industry moves"
  • "Market indicators"
  • "Viewpoints"
    • "Analyst view"
    • "Executive view"
    • "Interview"

I have a taxonomy.php theme file and I want these pages to be accessible at

(parent term) but also eg.

(sub-terms)

I have a $rewrite. When I set 'hierarchical' => false, of course, I can only access those sub-terms at their single-level URLs eg.

When I flip it to 'hierarchical' => true, I can successfully access the page at the nested URL structure

But (and here is the problem), this messes up the page for the parent term. Specifically, then generates a 404 (actually, points back to index).

How can I both have my cake and eat it - that is, how can I access a page for both sub-terms and their parent term?

At each step, I have saved permalinks in the admin.

Here is the code I am using to register the taxonomy in a plugin...

<?php
/**
 * ==============================================================================
 *                      REGISTER TAXONOMY
 * ==============================================================================
 */

if ( ! function_exists( 'register_taxonomy_type' ) ) {

function register_taxonomy_type() {

  $labels = array(
        'name'                       => _x( 'Types', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Types', 'text_domain' ),
        'all_items'                  => __( 'All Types', 'text_domain' ),
        'parent_item'                => __( 'Parent Type', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
        'new_item_name'              => __( 'New Type Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Type', 'text_domain' ),
        'edit_item'                  => __( 'Edit Type', 'text_domain' ),
        'update_item'                => __( 'Update Type', 'text_domain' ),
        'view_item'                  => __( 'View Type', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove Type', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular Type', 'text_domain' ),
        'search_items'               => __( 'Search Type', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No Type', 'text_domain' ),
        'items_list'                 => __( 'Types list', 'text_domain' ),
        'items_list_navigation'      => __( 'Types list navigation', 'text_domain' ),
        );
    $rewrite = array(
        'slug'                       => 'type',
        'with_front'                 => true,
        'hierarchical'               => false,
    );
        $args = array(
            'labels'                     => $labels, // as above
            'public'                     => true,
            'show_ui'                    => true,
            'show_in_nav_menus'          => true,
            'hierarchical'               => true,
            'show_admin_column'          => true,
        'single_value'               => false, // Use single-select radio button, only one Type per object
            'show_tagcloud'              => true,
            'rewrite'                    => $rewrite,// as above
        );
    // Put it all together!
    register_taxonomy(
        /* taxonomy name */         'type',
        /* attach to object */  array( 'article' ),
        /* arguments */                 $args
    );

}
add_action( 'init', 'register_taxonomy_type', 0 );

}
?>
Post a comment

comment list (0)

  1. No comments so far