【发布时间】:2014-08-19 03:49:13
【问题描述】:
我已经为此苦苦挣扎了几天,但似乎离解决方案还差得远。我一直在搜索论坛和教程网站,但最终变得更加困惑,因为似乎有很多方法和变体可以实现我正在寻找的东西。
我想做的是创建一个自定义帖子类型存档,可以根据 url 字符串按分类术语过滤。
_domain/products/_
_domain/products/taxonomy-term/_
_domain/products/taxonomy-term/product-1_
因此分类术语只会显示该类型的自定义帖子。
我已经做到了。这似乎适用于 domain/products/taxonomy_term/product_1,但没有选择任何存档模板。
// define custom post types
add_action( 'init', 'create_products' );
function create_products() {
register_post_type( 'products',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
'rewrite' => array('slug' => 'products/%product_cat%', 'with_front' => true ),
'hierarchical' => true,
'query_var' => true,
'show_in_nav_menus' => true,
'menu_position' => 5
)
);
}
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
register_taxonomy(
'product_cat',
'products',
array(
'labels' => array(
'name' => 'Product Categories',
'add_new_item' => 'Add New Product',
'new_item_name' => "New Product Category"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'products', 'with_front' => true ),
'query_var' => true,
)
);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'product_listing')
return $link;
if ($cats = get_the_terms($post->ID, 'product_cat'))
$link = str_replace('%product_cat%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
【问题讨论】:
-
你遇到这个问题了吗,有同样的问题吗?
-
我遇到了确切的问题。你解决过这个问题吗?
-
我已经经历了每一个,我的意思是每一个关于“自定义帖子类型和分类档案”主题的 SO 和 WP.SE 帖子,并尝试了每个解决方案的每一个小变化。几乎所有人都说,如果您使用
'has_archive' => 'post_type_slug'和'rewrite' => array( 'slug' => 'post_type_slug/%tax_slug%' )'设置帖子类型,然后使用'rewrite' => array( 'slug' => 'post_type_slug' )设置分类并包含那个小的post_type_link过滤器,它应该可以工作。.../post_type_slug/tax_term抛出 404。我确实有一个 taxonomy-tax_slug.php 文件。 -
我想你现在应该已经读过了,但是,here it is anyway
标签: wordpress filtering custom-post-type taxonomy