Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在WordPress中更改自定义帖子类型类别链接_Php_Wordpress_Custom Post Type - Fatal编程技术网

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

Php 如何在WordPress中更改自定义帖子类型类别链接,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,嗨,我正在用自定义帖子类型构建wordpress应用程序。在自定义帖子类型方面有问题 这是我下面自定义帖子类型的代码 $labels = array( 'name' => _x( 'Movies', 'movie' ), 'singular_name' => _x( 'movie', 'movie' ), 'add_new' => _x( 'Add New', 'movie' ), 'add_new_item' => _x( 'Add Ne

嗨,我正在用自定义帖子类型构建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重写为
集合
。但在我的收藏页面下,我将显示所有电影类别。这是我的密码

$args = array( 'taxonomy' => 'category' );
  echo wp_list_categories( $args );
问题是类别链接显示如下
http://localhost/films/category/action/
但我想这样更改它
http://localhost/films/collection/category/action/

您可以使用WordPress中提供的过滤器来实现这一点

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);
检查哪一个可能对你有帮助

它也值得一看