为什么在没有任何条款的情况下尝试访问 taxonomy.php 页面时出现 404 错误(Wordpress 看不到分类法页面)
Why am I getting a 404 error when trying to reach the taxonomy.php page without any terms, (Wordpress don't see taxonomy page)
我正在尝试创建分类法页面,所以我使用 taxonomie taxonomy-nowe.php
创建了分类法
但是我的 WP 看不到该页面,或者我在重写时搞砸了一些东西 URL?有人可以检查我的代码,看看我做错了什么。我通过保存 Plain 然后 return 到 Post 名称永久链接来刷新永久链接。但是我没有得到结果。
我想实现这样的想法:域。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 );
这个 post 已经编辑了很多次,因为当时我没有关于这个问题的所有信息。从那以后,我对该主题进行了广泛的研究。下面直奔主题和答案
我们这里有两个目标,获得以下永久链接结构
domain.com/_custom_post_type_/_taxonomy_/_term_/ ...
并获取 taxonomy.php
到 return post 的索引和与自定义 post 类型的特定分类法部分 a 相关联的术语,而不是 404.php
页。
"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
在您搜索 ./fruits/apples
的情况下,您会在 taxonomy.php
或 [=22= 上显示与词条 apples
相关的所有 post ] 或最终 taxonomy-fruits-aples.php
。
但是如果你想访问 ./fruits/
怎么办? 为什么访问水果会返回 404.php
错误? 好吧,因为 taxonomy.php
是为了显示 posts 反对条款,而不是 post反对分类法。 这是正常的预期行为。
这可以追溯到 10 年前 https://core.trac.wordpress.org/ticket/13816,并提出了多个问题。 (有趣的阅读,你应该看看)。
当您搜索 ./fruits/apples/
时,我们会得到一个 "true"
,但是对于 ./fruits/
,如果用户搜索 [=24],我们会得到一个 "false"
,考虑到该逻辑=] 或 ./fruits
他应该被重定向到包含所有相关 fruits
post 或术语的模板,因为这不是默认行为,我提出了一个个案的解决方案:
<?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
,值应与您的分类 slug 等于 WITH 破折号的字符数相匹配。 substr( $url, -7 ) == '/fruits'
这可能不是最好的方法,但它可以完成工作并且速度非常快。我没有看到任何其他有效的解决方案(2020 年 9 月)。
其中 fruits.php
是您的后备模板,以防用户搜索 ./fruits/
或 ./fruits
。我们取 url 的末尾以将其与我们的自定义分类法 slug 相匹配。这应该只发生在 is_tax()
为 false ! is_tax( 'fruits' )
并且 url 匹配我们的分类法时。
现在假设我们的自定义分类与名为“recipes”的自定义 post 类型相关联。然后,我们可以使用以下内容在我们的后备 fruit.php
模板文件中显示 posts 和自定义 post 类型的相关术语:
<?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;
};
}; ?>
此外,您还希望在固定链接中的分类法前面为您的自定义分类法设置自定义 post 类型 (CPT),以获得如下结构:
recipes/fruits/apples
其中 recipes 是您的自定义 post 类型。您可以在使用以下内容注册该分类法的参数时使用重写规则:'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_/ ...
分类页面现在可以用作 post 和分类索引。
我正在尝试创建分类法页面,所以我使用 taxonomie taxonomy-nowe.php
创建了分类法但是我的 WP 看不到该页面,或者我在重写时搞砸了一些东西 URL?有人可以检查我的代码,看看我做错了什么。我通过保存 Plain 然后 return 到 Post 名称永久链接来刷新永久链接。但是我没有得到结果。
我想实现这样的想法:域。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 );
这个 post 已经编辑了很多次,因为当时我没有关于这个问题的所有信息。从那以后,我对该主题进行了广泛的研究。下面直奔主题和答案
我们这里有两个目标,获得以下永久链接结构
domain.com/_custom_post_type_/_taxonomy_/_term_/ ...
并获取 taxonomy.php
到 return post 的索引和与自定义 post 类型的特定分类法部分 a 相关联的术语,而不是 404.php
页。
"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
在您搜索 ./fruits/apples
的情况下,您会在 taxonomy.php
或 [=22= 上显示与词条 apples
相关的所有 post ] 或最终 taxonomy-fruits-aples.php
。
但是如果你想访问 ./fruits/
怎么办? 为什么访问水果会返回 404.php
错误? 好吧,因为 taxonomy.php
是为了显示 posts 反对条款,而不是 post反对分类法。 这是正常的预期行为。
这可以追溯到 10 年前 https://core.trac.wordpress.org/ticket/13816,并提出了多个问题。 (有趣的阅读,你应该看看)。
当您搜索 ./fruits/apples/
时,我们会得到一个 "true"
,但是对于 ./fruits/
,如果用户搜索 [=24],我们会得到一个 "false"
,考虑到该逻辑=] 或 ./fruits
他应该被重定向到包含所有相关 fruits
post 或术语的模板,因为这不是默认行为,我提出了一个个案的解决方案:
<?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
,值应与您的分类 slug 等于 WITH 破折号的字符数相匹配。 substr( $url, -7 ) == '/fruits'
这可能不是最好的方法,但它可以完成工作并且速度非常快。我没有看到任何其他有效的解决方案(2020 年 9 月)。
其中 fruits.php
是您的后备模板,以防用户搜索 ./fruits/
或 ./fruits
。我们取 url 的末尾以将其与我们的自定义分类法 slug 相匹配。这应该只发生在 is_tax()
为 false ! is_tax( 'fruits' )
并且 url 匹配我们的分类法时。
现在假设我们的自定义分类与名为“recipes”的自定义 post 类型相关联。然后,我们可以使用以下内容在我们的后备 fruit.php
模板文件中显示 posts 和自定义 post 类型的相关术语:
<?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;
};
}; ?>
此外,您还希望在固定链接中的分类法前面为您的自定义分类法设置自定义 post 类型 (CPT),以获得如下结构:
recipes/fruits/apples
其中 recipes 是您的自定义 post 类型。您可以在使用以下内容注册该分类法的参数时使用重写规则:'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_/ ...
分类页面现在可以用作 post 和分类索引。