简体   繁体   English

如何在WordPress中更改自定义帖子类型类别链接

[英]How to change custom post type category link in WordPress

Hi I'm building a wordpress application with custom post type. 嗨,我正在构建具有自定义帖子类型的wordpress应用程序。 Having trouble with custom post type. 自定义帖子类型遇到问题。

This is my following code for custom post type 这是我的自定义帖子类型的以下代码

$labels = array(
    'name' => _x( 'Movies', 'movie' ),
    'singular_name' => _x( 'movie', 'movie' ),
    'add_new' => _x( 'Add New', 'movie' ),
    'add_new_item' => _x( 'Add New movie', 'movie' ),
    'edit_item' => _x( 'Edit movie', 'movie' ),
    'new_item' => _x( 'New movie', 'movie' ),
    'view_item' => _x( 'View movie', 'movie' ),
    'search_items' => _x( 'Search Movies', 'movie' ),
    'not_found' => _x( 'No Movies found', 'movie' ),
    'not_found_in_trash' => _x( 'No Movies found in Trash', 'movie' ),
    'parent_item_colon' => _x( 'Parent movie:', 'movie' ),
    'menu_name' => _x( 'Movies', 'movie' ),
);

$args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'movie Collections',
    'supports' => array( 'title', 'editor', 'thumbnail' ),
    'taxonomies' => array( 'category', 'page-category' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 20,
    'menu_icon' => get_template_directory_uri().'/images/movie.png',
    'show_in_nav_menus' => false,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => array(
          'slug'=>'collection',
     ),
    'capability_type' => 'page'

);

register_post_type( 'movie', $args );

I've rewrite the post type URL to collections . 我已经将帖子类型的URL重写为collections but under my collection page, I'm displaying all movies category. 但在我的收藏页下,我正在显示所有电影类别。 This is my code 这是我的代码

$args = array( 'taxonomy' => 'category' );
  echo wp_list_categories( $args );

Problem is the category link is displaying like this http://localhost/films/category/action/ But I want to change it like this http://localhost/films/collection/category/action/ 问题是类别链接像这样显示http://localhost/films/category/action/但我想像这样更改它http://localhost/films/collection/category/action/

you achieve this using post_type_link filter available in WordPress 您可以使用WordPress中可用的post_type_link过滤器实现此目的

function filter_post_type_link($link, $post) {
    if ($post->post_type != 'movie')
        return $link;

    if ($cats = get_the_terms($post->ID, 'movie'))
        $link = str_replace('%category%', array_pop($cats)->slug, $link);
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

Check this answer here which may help you 在此处检查此答案,这可能对您有帮助

It is also worth looking term_link filter 还值得一看term_link过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 链接到自定义帖子类型类别 - Link to custom post type category 如何在wordPress中将自定义帖子类型类别链接到其各自的存档 - How to link my custom post type category to its respective archive in wordPress 如何:Wordpress中自定义帖子类型的自定义类别存档页面 - How to: Custom Category Archive Page for custom post type in wordpress 尝试获取当前帖子的 Wordpress 中的自定义帖子类型类别链接作为帖子的元数据 - Trying to get the custom Post type category link in Wordpress for the current post as meta data of the post 如何获取在Wordpress中按类别过滤的自定义帖子类型的永久链接? - How to get permalink of custom post type that filter by category in Wordpress? 如何通过ID从Wordpress查询中的自定义帖子类型中获取类别 - How to get category by ID from a custom post type in a Wordpress query 如何在wordpress中的自定义帖子类型中加载默认类别值 - How to load default category value in custom post type in wordpress 如何在WordPress自定义帖子类型中按类别显示帖子 - how to show posts by category in WordPress custom post type 如何在wordpress中更改自定义帖子类型slug - How to change custom post type slug in wordpress Wordpress:如何将 ACF 自定义帖子类型链接到 Wordpress 帐户 - Wordpress: How to link ACF Custom post type to Wordpress account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM