【问题标题】:Custom post type slug conflicts with another post type自定义帖子类型 slug 与其他帖子类型冲突
【发布时间】:2017-09-19 22:05:55
【问题描述】:

这让我发疯了。

我有两种自定义帖子类型

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 类型的 URL 是 www.website.com/products/a-market-post,这真的很奇怪,因为它使用的是产品帖子类型中的 slug。

有人知道会发生什么吗?我已经刷新了永久链接页面 1000 次,但没有任何作用。

干杯!

【问题讨论】:

  • 你的永久链接结构是什么?
  • 你把上面的代码放在哪里了?在初始化钩子上触发的函数内?
  • @MohammadAshiqueAli 月份和名称
  • @NathanDawson 我已经尝试过直接运行functions.php,也尝试过在init触发的函数中

标签: php wordpress


【解决方案1】:

您缺少“分类法”。
而且您不需要“重写”,因为您的 slug 保持不变。

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' );

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 2021-08-21
    • 2021-10-17
    • 2022-01-22
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2013-01-28
    相关资源
    最近更新 更多