簡體   English   中英

如何在WordPress中更改自定義帖子類型類別鏈接

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

嗨,我正在構建具有自定義帖子類型的wordpress應用程序。 自定義帖子類型遇到問題。

這是我的自定義帖子類型的以下代碼

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

我已經將帖子類型的URL重寫為collections 但在我的收藏頁下,我正在顯示所有電影類別。 這是我的代碼

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

問題是類別鏈接像這樣顯示http://localhost/films/category/action/但我想像這樣更改它http://localhost/films/collection/category/action/

您可以使用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);

在此處檢查此答案,這可能對您有幫助

還值得一看term_link過濾器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM