为什么在尝试访问没有任何术语的 taxonomy.php 页面时出现 404 错误,(Wordpress 看不到分类页面)
我正在尝试创建分类法页面,所以我使用分类法 taxonomy-nowe.php 创建了分类法
I'm trying to create taxonomy page so I created taxonomy with taxonomie taxonomy-nowe.php
但是我的 WP 没有看到该页面,或者我在重写 URL 上弄乱了一些东西?有人可以检查我的代码,看看我做错了什么.我通过保存平原来刷新永久链接,然后返回到帖子名称永久链接.但我没有得到结果.
but my WP don't see the page or I mess something with the rewrite URL? Can someone check my code and see if something I'm doing wrong. I did the flush the permalinks by save the Plain and then return to Post name permalinks. But I get not result.
我想实现这样的想法:domain.pl/oferta/kopiarki/nowe
I want to achive somethink like this: domain.pl/oferta/kopiarki/nowe
// custom post type
function td_devices_posttype() {
$labels = array(
'name' => _x( 'Kopiarki', 'Ogólna nazwa wpisów', 'textdomain' ),
'singular_name' => _x( 'Kopiarka', 'Pojedyńcza nazwa wpisu', 'textdomain' ),
'menu_name' => esc_html__( 'Kopiarki', 'textdomain' ),
'parent_item_colon' => esc_html__( 'Rodzic', 'textdomain' ),
'all_items' => esc_html__( 'Wszystkie kopiarki', 'textdomain' ),
'view_item' => esc_html__( 'Wyświetl kopiarki', 'textdomain' ),
'add_new_item' => esc_html__( 'Dodaj nową kopiarkę', 'textdomain' ),
'add_new' => esc_html__( 'Dodaj nową', 'textdomain' ),
'edit_item' => esc_html__( 'Edytuj Kopiarkę', 'textdomain' ),
'update_item' => esc_html__( 'Zaktualizuj kopiarkę', 'textdomain' ),
'search_items' => esc_html__( 'Szukaj kopiarkę', 'textdomain' ),
'not_found' => esc_html__( 'Nie zanleziono', 'textdomain' ),
'not_found_in_trash' => esc_html__( 'Nie znaleziono w koszu', 'textdomain' )
);
$args = array(
'label' => esc_html__( 'kopiarki', 'textdomain' ),
'description' => esc_html__( 'Wszystkie kopiarki', 'textdomain' ),
'labels' => $labels,
'taxonomies' => array( 'nowe' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 100,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => true,
'show_admin_column' => true,
'capability_type' => 'page',
'rewrite' => array('slug' => 'oferta/kopiarki'),
'supports' => array( 'title','editor','thumbnail', 'custom-fields')
);
register_post_type( 'kopiarki', $args );
//flush_rewrite_rules();
}
add_action( 'init', 'td_devices_posttype' );
// custom taxonomies new
function new_posttype_taxonomy() {
$labels = array(
'name' => 'Nowe',
'singular_name' => 'Nowa',
'search_items' => 'Szukaj nowych',
'all_items' => 'Wszystkie nowe kopiarki',
'parent_item' => 'Rodzic',
'parent_item_colon' => 'Dwukropek elementu nadrzędnego:',
'edit_item' => 'Edytuj nową kopiarkę',
'update_item' => 'Zaktualizuj nową kopiarkę',
'add_new_item' => 'Dodaj nową kopiarkę',
'new_item_name' => 'Nowa nazwa kopiarki',
'menu_name' => 'Nowe'
);
$args = array (
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'nowe',
'with_front' => false,
)
);
register_taxonomy( 'nowe', array( 'kopiarki' ), $args );
推荐答案
这篇文章已经被编辑了很多次,因为当时我没有关于这个问题的所有信息.从那以后,我对该主题进行了广泛的研究.以下内容直接切入正题和答案.
我们在这里有两个目标,获得以下永久链接结构domain.com/_custom_post_type_/_taxonomy_/_term_/...
We have two goals here, getting the permalink structure to the following
domain.com/_custom_post_type_/_taxonomy_/_term_/ ...
并获取 taxonomy.php
以返回与自定义帖子类型的特定分类部分 a 相关联的帖子和术语的索引,而不是 404.php
页面.
And get the taxonomy.php
to return an index of posts and terms associated to a specific taxonomy part a of custom post type instead of a 404.php
page.
"当访问者点击指向类别、标签或自定义的超链接时分类法,WordPress 按时间倒序显示一个帖子页面由该分类法过滤的顺序."
"When a visitor clicks on a hyperlink to category, tag or custom taxonomy, WordPress displays a page of posts in reverse chronological order filtered by that taxonomy."
来源:@https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/
用于分类的默认模板是 taxonomy.php
.这些文件让您可以定位特定分类法或特定分类法术语.例如:taxonomy-{taxonomy}-{term}.php
和 taxonomy-{taxonomy}.php
The default template used for taxonomies is taxonomy.php
. These files let you target specific taxonomies or specific taxonomy terms. For example: taxonomy-{taxonomy}-{term}.php
and taxonomy-{taxonomy}.php
在您的情况下,当您搜索 ./fruits/apples
时,您会得到与 apples
一词相关的所有帖子,要么显示在 taxonomy.php上
code> 或 taxonomy-fruits.php
或最后 taxonomy-fruits-aples.php
.但是如果你想访问 ./fruits/
怎么办?为什么访问 Fruit 会返回 404.php
错误? 嗯,因为 taxonomy.php
旨在显示针对术语的帖子,而不是针对分类法的帖子.这是正常和预期的行为.
In your case when you search for ./fruits/apples
you get all posts related to the term apples
either displayed on either taxonomy.php
or on taxonomy-fruits.php
or finally on taxonomy-fruits-aples.php
.
But what if you want to access ./fruits/
? Why does accessing fruit gives back a 404.php
error? Well because taxonomy.php
was intended to displays posts against terms, not posts against taxonomies. This is the normal and intended behaviour.
这可以追溯到 10 年前https://core.trac.wordpress.org/ticket/13816,并提出了多个问题.(有趣的阅读,你应该看看).
This goes back to 10 years ago https://core.trac.wordpress.org/ticket/13816, and multiple issues were raised. (Interesting read, you should take a look).
当您搜索 ./fruits/apples/
时,我们会得到 true"
但使用 ./fruits/
我们会得到false"
考虑到该逻辑,如果用户搜索 ./fruits/
或 ./fruits
他应该被重定向到模板对于所有相关的 fruits
帖子或条款,因为这不是默认行为,我想出了一个逐案解决方案:
When you search for ./fruits/apples/
we get a "true"
but with ./fruits/
we get a "false"
with that logic in mind if a user search for ./fruits/
or ./fruits
he should be redirected to a template with all related fruits
posts or terms, as this is not default behaviour, I came up with a case by case solution:
<?php add_action( 'template_redirect', 'fallback_custom_taxonomy_fruits' );
function fallback_custom_taxonomy_fruits() {
$url = $_SERVER[ 'REQUEST_URI' ];
if ( ! is_tax( 'fruits' ) && substr( $url, -7 ) == '/fruits' || substr( $url, -8 ) == '/fruits/' ) {
include( get_template_directory() . '/fruits.php' );
exit();
};
}; ?>
关于 substr
,值应与您的分类标头等于 WITH 破折号的字符数相匹配.substr( $url, -7 ) == '/fruits'
Regarding substr
, value should match the number of characters your taxonomy slug is equal to WITH dashes. substr( $url, -7 ) == '/fruits'
这可能不是最好的方法,但它可以完成工作,而且速度非常快.我没有看到任何其他有效的解决方案(2020 年 9 月).
This is probably not the best way but it does the job and it is pretty quick. I didn't see any other solution that was working (September 2020).
其中 fruits.php
是您的后备模板,以防用户搜索 ./fruits/
或 ./fruits
.我们将 url 的末尾与我们的自定义分类 slug 进行匹配.这应该只在 is_tax()
为 false 时发生!is_tax( 'fruits' )
并且 url 与我们的分类相匹配.
Where fruits.php
is your fallback template in case a user search for ./fruits/
or ./fruits
. We take the end of the url to match it against our custom taxonomy slug. This should only happens when is_tax()
is false ! is_tax( 'fruits' )
and the url matches our taxonomy.
现在假设我们的自定义分类与称为食谱"的自定义帖子类型相关联.然后,我们可以使用以下内容在回退 fruit.php
模板文件中显示自定义帖子类型的帖子和相关术语:
Let's say now that our custom taxonomy is associated with a custom post type called "recipes". We can then use the following to display posts and associated terms for a custom post type on our fallback fruit.php
template file:
<?php
$custom_post_type = 'recipes';
$taxonomy = 'fruits';
$terms = get_terms( $taxonomy );
foreach ( $terms as $term ) {
wp_reset_query();
$args = array(
'post_type' => $custom_post_type,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
), );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo $term->name.'<br/>';
while ( $query->have_posts() ) : $query->the_post();
echo '<a href="'.get_permalink().'">'.the_title().'</a><br>';
endwhile;
};
}; ?>
此外,您还希望在您的永久链接中,在您的分类法之前,为您的自定义分类法设置自定义帖子类型 (CPT),以获得如下结构:recipes/fruits/apples
其中,recipes 是您的自定义帖子类型.您可以在使用以下内容注册该分类法的参数时使用重写规则: 'rewrite' =>数组('slug' => '食谱/水果'),分层=>真, ),
.
Additionally you also wanted to have the custom post type (CPT) for your custom taxonomy, in your permalink, in front of your taxonomy, to get a structure like so:
recipes/fruits/apples
where recipes is your custom post type. You can use the rewrite rule upon registering that taxonomy's arguments using the following: 'rewrite' => array( 'slug' => 'recipes/fruits' ), hierarchical => true, ),
.
add_action( 'init', 'custom_taxonomy_nowe' );
function custom_taxonomy_nowe() {
$custom_post_type = 'Recipes';
$singular = 'Fruit';
$plural = 'Fruits';
$labels = array(
'name' => $plural,
'singular_name' => $singular,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'rewrite' => array( 'slug' => strtolower( $custom_post_type.'/'.$plural ), 'hierarchical' => true, ),
);
register_taxonomy( strtolower( $plural ), strtolower( $custom_post_type ), $args );
};
我们的永久链接结构现在应该如下所示domain.com/recipes/fruits/apples/...
domain.com/_custom_post_type_/_taxonomy_/_term_/...
Our permalink structure should now look like the following
domain.com/recipes/fruits/apples/ ...
domain.com/_custom_post_type_/_taxonomy_/_term_/ ...
分类页面现在可以用作帖子和分类索引.
The taxonomy page can now be use as a posts and a taxonomies index.
相关文章