尝试不带任何术语进入taxonomy.php页面时,为什么会出现404错误,(Wordpress看不到taxonomy页面) [英] Why am I getting a 404 error when trying to reach the taxonomy.php page without any terms, (Wordpress don't see taxonomy page)
问题描述
我正在尝试创建分类法页面,所以我使用分类法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
时,您会在 taxonomy.php 上看到与
apples
相关的所有帖子.代码>或在 taxonomy-fruits.php
上,或者最后在 taxonomy-fruits-aples.php
上.但是,如果您想访问 ./fruits/
,该怎么办?为什么访问水果会返回 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)=='/水果'
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的末尾与我们的自定义分类标准段匹配.仅当 is_tax()
为错误的时才会发生这种情况!is_tax('fruits')
,并且该网址与我们的分类法匹配.
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),以获得如下结构:食谱/水果/苹果
,其中食谱是您自定义的帖子类型.在使用以下方法注册该分类法的参数时,可以使用重写规则:'rewrite'=>array('slug'=>'recipes/fruits'),分层=>true,),
.
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.
这篇关于尝试不带任何术语进入taxonomy.php页面时,为什么会出现404错误,(Wordpress看不到taxonomy页面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!