admin管理员组

文章数量:1193728

I have several custom post types on a site where top-level nav items use archive-{post-type}.php templates of the post types. These have recently had a template problem and they now use the index.php template, instead of their archive template. This is possibly after I added a custom taxonomy to the end of functions.php (below), but only because this is the most recent significant change to functions. They still use the correct archive templates on our production site, but not on the development site, where changes have been made.

I've tried removing the taxonomy action, no change.

I've tried saving permalinks in admin settings, no change.

Also maybe relevant: this also only affects top-level templates - there are several custom post types with rewrites to eg domain/media-center/articles - and the articles for these work as expected.

I've not faced this issue before and haven't yet found any solutions that resolve it. Is there anything that I can check that might be causing it - or a way to get to the bottom of the issue?

The recently added taxonomy:

add_action( 'init', 'create_year_taxonomy' );
function create_year_taxonomy() {
  
  // translations for GUI
  $labels = array(
    'name' => _x( 'Edition Year', 'taxonomy general name' ),
    'singular_name' => _x( 'Year', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Years' ),
    'all_items' => __( 'All Years' ),
    'parent_item' => __( 'Parent Year' ),
    'edit_item' => __( 'Edit Year' ), 
    'update_item' => __( 'Update Year' ),
    'add_new_item' => __( 'Add New Year' ),
    'new_item_name' => __( 'New Year' ),
    'menu_name' => __( 'Years' ),
  );    
  
  // register the taxonomy
  register_taxonomy('year',array('the_street_view'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'year' ),
  ));
}

I have several custom post types on a site where top-level nav items use archive-{post-type}.php templates of the post types. These have recently had a template problem and they now use the index.php template, instead of their archive template. This is possibly after I added a custom taxonomy to the end of functions.php (below), but only because this is the most recent significant change to functions. They still use the correct archive templates on our production site, but not on the development site, where changes have been made.

I've tried removing the taxonomy action, no change.

I've tried saving permalinks in admin settings, no change.

Also maybe relevant: this also only affects top-level templates - there are several custom post types with rewrites to eg domain.com/media-center/articles - and the articles for these work as expected.

I've not faced this issue before and haven't yet found any solutions that resolve it. Is there anything that I can check that might be causing it - or a way to get to the bottom of the issue?

The recently added taxonomy:

add_action( 'init', 'create_year_taxonomy' );
function create_year_taxonomy() {
  
  // translations for GUI
  $labels = array(
    'name' => _x( 'Edition Year', 'taxonomy general name' ),
    'singular_name' => _x( 'Year', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Years' ),
    'all_items' => __( 'All Years' ),
    'parent_item' => __( 'Parent Year' ),
    'edit_item' => __( 'Edit Year' ), 
    'update_item' => __( 'Update Year' ),
    'add_new_item' => __( 'Add New Year' ),
    'new_item_name' => __( 'New Year' ),
    'menu_name' => __( 'Years' ),
  );    
  
  // register the taxonomy
  register_taxonomy('year',array('the_street_view'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'year' ),
  ));
}
Share Improve this question asked Sep 13, 2022 at 17:43 MikeMike 233 bronze badges 4
  • Sounds like you've tried the most obvious options (permalinks, remove added code) an obvious next step is to copy prod to dev again but I'm guessing you have other changes to retain in dev. If you are using Git for managing your code, a git bisect could help isolate the offending code issue. – jdm2112 Commented Sep 13, 2022 at 19:24
  • 1 Though you don't mention changes of the sort template_include filters and rewrite rules are both plausible culprits for this behavior. The former can change which template file is loaded for a query and the latter can change a query which is produced for a URL - so either might result in a different template being loaded at the same URL. A new taxonomy registration is unlikely to be the source. You might var_dump() the $wp_query global at the top of the erroneous template in order to examine the query (or use something like Query Monitor) and ensure it is as you would expect for that URL – bosco Commented Sep 13, 2022 at 19:46
  • @bosco this is really helpful, thanks. It shows that a page displaying correctly has this query: post_type=offices but a page displaying incorrectly shows the query: year=about-us. This appears to be affected by my latest taxonomy (in original question) in some way - but I'm not sure why this is happening or how to resolve. I tried removing the taxonomy from functions (and nuking all functions content as a test) but the issue remained. Any ideas for next steps? – Mike Commented Sep 14, 2022 at 9:17
  • 1 @bosco further, issue fixed: removed the taxonomy in functions AND updated permalinks and it's resolved. This was all thanks to using Query Monitor to understand what was being requested - so thank you for that tip and have added it to my essential tools! – Mike Commented Sep 14, 2022 at 9:27
Add a comment  | 

1 Answer 1

Reset to default 1

The issue was, in fact, the new taxonomy using "year" clashes with the query.

Wordpress was accepting year=about-us in the query chain for instance, as year was a taxonomy, before the expected page=about-us - preventing it from appearing as a page and using the index.php template as there is no "year" template.

I changed year to a more custom slug edition-year in functions.php and flushed the permalinks, which resolved the issue.

本文标签: