【问题标题】:WordPress Permalink Structure for Custom Post Type自定义帖子类型的 WordPress 永久链接结构
【发布时间】:2026-02-04 00:15:02
【问题描述】:

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

我目前有一个名为“产品”的自定义帖子类型,其 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' );

我已启用右侧的类别部分,以便在选中一个类别时,我无法获得该类别以在Permalink结构中出现。

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

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

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

【问题讨论】:

    标签: php wordpress wordpress-theming backend permalinks


    【解决方案1】:

    您可以添加过滤器到post_type_link

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

    【讨论】:

      【解决方案2】:

      转到设置 -> 永久链接向下滚动到产品永久链接并选择通用设置并将其添加到自定义结构 /%taxonomyname%/posttypename/

      【讨论】:

      • 感谢您的回复。不幸的是,出于某种原因,我的永久链接页面上没有“产品永久链接”。这可能是什么原因?在这里查看我的截图 - imgur.com/7OjhFEF
      • 哦,对不起,我还以为你安装了 woo commerce。我将编辑我的答案。
      • 你的分类名称是什么?
      最近更新 更多