首页 > 解决方案 > 自定义帖子类型的 WordPress 永久链接结构

问题描述

我正在建立一个网站,但不知道如何仅为我的自定义帖子类型更改永久链接结构!

我目前有一个名为“产品”的自定义帖子类型,其 URL 结构如下

/products/continental-rack/

我的自定义帖子类型的完整代码如下:

function cpt_products() {
  $labels = array(
    'name'               => 'Products',
    'singular_name'      => 'Product',
    'menu_name'          => 'Products',
    'name_admin_bar'     => 'Product',
    'add_new'            => 'Add New',
    'add_new_item'       => 'Add New Product',
    'new_item'           => 'New Product',
    'edit_item'          => 'Edit Product',
    'view_item'          => 'View Product',
    'all_items'          => 'All Products',
    'search_items'       => 'Search Products',
    'parent_item_colon'  => 'Parent Product',
    'not_found'          => 'No Products Found',
    'not_found_in_trash' => 'No Products Found in Trash'
  );

  $args = array(
    'labels'              => $labels,
    'public'              => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'show_ui'             => true,
    'show_in_nav_menus'   => true,
    'show_in_menu'        => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-tag',
    'capability_type'     => 'post',
    'taxonomies'          => array( 'category' ),
    'hierarchical'        => false,
    'supports'            => array( 'title', 'author', 'thumbnail' ),
    'has_archive'         => true,
    'rewrite'             => array( 'slug' => 'products' ),
    'query_var'           => true
  );

  register_post_type( 'products', $args );
}
add_action( 'init', 'cpt_products' );

我已经启用了右侧的类别部分,因此我可以选择一个类别,但是当检查一个类别时,我无法将该类别显示在永久链接结构中。

所以我想要的永久链接结构本质上是这样的:

/%category-name%/products/continental-rack/

就是找不到办法。任何帮助,将不胜感激!

标签: phpwordpresswordpress-themingbackendpermalinks

解决方案


您可以将过滤器添加到post_type_link

这是正确的方法:https ://wordpress.stackexchange.com/a/22490


推荐阅读