2

我已经为此苦苦挣扎了几天,似乎离解决方案还很近。我一直在搜索论坛和教程网站,但最终变得更加困惑,因为似乎有很多方法和变化可以实现我正在寻找的东西。

我想要做的是创建一个自定义帖子类型存档,可以根据 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);
4

1 回答 1

0

对我来说,这是命名的冲突。我有一个与分类同名的帖子类型。这引起了冲突。

register_post_type( 'some_name' );
register_taxonomy( 'some_name' );

将分类重命名为独特的名称。

register_taxonomy( 'some_tax_name' );
于 2014-08-18T19:49:13.930 回答