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
|
1 Answer
Reset to default 1The 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.
本文标签:
版权声明:本文标题:functions - Custom Post Types using wrong template (index) instead of archive-{type}.php - previously worked as expected 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738427199a2086195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 mightvar_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:46post_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