从固定链接中删除CPT名称,但改为添加%CATEGOR%

时间:2018-05-22 作者:Wouter

我们目前正在开发一个新的wp主题,所有当前的帖子都转移到一个名为products的新CPT。对于SEO,需要保留产品的链接。

该职位类型的当前结构为:

www.url。com/category-a/category-b/productname

将项目从默认帖子类型移动到新的CPT后,URL如下所示:

www.url。com/products/productname

但我们希望保留这些职位的默认职位结构。归档页面显示产品,因此工作正常。

我的永久链接设置为:

/%类别%/%pagename%

因此,我们基本上想要改变:www.url.com/products/productname

收件人:www.url.com/category-a/subcategory-a/productname

这是我们创建的CPT:

function cptui_register_my_cpts_producten() {

    /**
     * Post Type: Producten.
     */

    $labels = array(
        "name" => __( "Producten", "sage" ),
        "singular_name" => __( "Product", "sage" ),
        "archives" => __( "Producten Archief", "sage" ),
    );

    $args = array(
        "label" => __( "Producten", "sage" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "producten", "with_front" => false ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail" ),
        "taxonomies" => array( "category" ),
    );

    register_post_type( "producten", $args );
}

add_action( \'init\', \'cptui_register_my_cpts_producten\' );

1 个回复
SO网友:nazim

你必须设置"hierarchical" => true 为了得到%category%%subcategory% 要显示:

"rewrite" => array(
    "slug" => "producten",
    "with_front" => false,
    "hierarchical" => true
),
不要忘记通过打开刷新永久链接设置settings →permalinks 并保存。

更多信息:WordPress Codex: Function Reference/register post type

结束

相关推荐

Problem with permalinks

我已经更改了类别的基本名称,现在它是“博客”,工作正常。但当我通过/blog/%category%/%postname%/更改结构时。显示404。如果我删除结构中的blog,它会再次工作,但我想使用blog word。问题出在哪里?非常感谢。