自定义帖子类型slug与另一个帖子类型冲突

时间:2017-04-23 04:53:34

标签: php wordpress

这让我疯了。

我有两种自定义帖子类型

register_post_type( 'products',
    array(
        'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'products'),
    )
);

register_post_type( 'markets',
    array(
        'labels' => array(
            'name' => __( 'Markets' ),
            'singular_name' => __( 'Market' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'markets'),
    )
);

和两个模板(archive-products.php和archive-markets.php)

products自定义类型有效。存档页面显示正确,但不显示单页。如果我删除了市场的register_post_type,那么产品的单页就可以使用。

但是markets类型的网址为www.website.com/products/a-market-post,这非常奇怪,因为它使用了产品帖子类型中的slug。

有谁知道可能会发生什么?我已经刷新永久链接页面1000次,但没有做任何事情。

干杯!

1 个答案:

答案 0 :(得分:0)

您错过了分类标准' 而且你不需要重写""因为你的slu are保持不变。

function manufacturers_posts() {
    $labels = array(
        'name'               => _x( 'Products', 'post type general name' ),
        'singular_name'      => _x( 'Product', 'post type singular name' ),
        'add_new'            => _x( 'Add new', 'book' ),
        'add_new_item'       => __( 'Add new product' ),
        'edit_item'          => __( 'Edit' ),
        'new_item'           => __( 'יAdd new' ),
        'all_items'          => __( 'Show all' ),
        'view_item'          => __( 'Show' ),
        'search_items'       => __( 'Search' ),
        'not_found'          => __( 'No products found' ),
        'not_found_in_trash' => __( 'Trash is empty' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Products'
    );

    $args = array(
        'labels'        => $labels,
        'public'        => true,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'revisions', 'custom-fields', 'tags' ),
        'has_archive'   => true,
        'taxonomies' => array('post_tag')
    );

    register_post_type( 'manufacturers', $args );
}

function manufacturers_taxonomy() {
    register_taxonomy(
        'manufacturers_taxonomy',                    //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
        'manufacturers',                             //post type name
        array(
            'hierarchical' => true,
            'label' => 'Products',                       //Display name
            'query_var' => true,
            'has_archive' => 'manufacturers',
            'rewrite' => array(
                'slug' => 'manufacturers-archive',   // This controls the base slug that will display before each term
                'with_front' => false                // Don't display the category base before
            )
        )
    );
}

    add_action( 'init', 'manufacturers_taxonomy');
    add_action( 'init', 'manufacturers_posts' );