个性化阅读
专注于IT技术分析

WordPress:分层标签端点的问题

我的一位同事按照本教程的内容在wordpress上进行了分层标记:https://css-tricks.com/how-and-why-to-convert-wordpress-tags-from-flat-to-hierarchical

这是我的孩子主题的functions.php上的部分

function wd_hierarchical_tags_register() {



  // Maintain the built-in rewrite functionality of WordPress tags



  global $wp_rewrite;



  $rewrite =  array(

    'hierarchical'              => false, // Maintains tag permalink structure

    'slug'                      => get_option('tag_base') ? get_option('tag_base') : 'tag', 'with_front'                => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(), 'ep_mask'                   => EP_TAGS, );



  // Redefine tag labels (or leave them the same)



  $labels = array(

    'name'                       => _x( 'Tags', 'Taxonomy General Name', 'hierarchical_tags' ), 'singular_name'              => _x( 'Tag', 'Taxonomy Singular Name', 'hierarchical_tags' ), 'menu_name'                  => __( 'Taxonomy', 'hierarchical_tags' ), 'all_items'                  => __( 'All Tags', 'hierarchical_tags' ), 'parent_item'                => __( 'Parent Tag', 'hierarchical_tags' ), 'parent_item_colon'          => __( 'Parent Tag:', 'hierarchical_tags' ), 'new_item_name'              => __( 'New Tag Name', 'hierarchical_tags' ), 'add_new_item'               => __( 'Add New Tag', 'hierarchical_tags' ), 'edit_item'                  => __( 'Edit Tag', 'hierarchical_tags' ), 'update_item'                => __( 'Update Tag', 'hierarchical_tags' ), 'view_item'                  => __( 'View Tag', 'hierarchical_tags' ), 'separate_items_with_commas' => __( 'Separate tags with commas', 'hierarchical_tags' ), 'add_or_remove_items'        => __( 'Add or remove tags', 'hierarchical_tags' ), 'choose_from_most_used'      => __( 'Choose from the most used', 'hierarchical_tags' ), 'popular_items'              => __( 'Popular Tags', 'hierarchical_tags' ), 'search_items'               => __( 'Search Tags', 'hierarchical_tags' ), 'not_found'                  => __( 'Not Found', 'hierarchical_tags' ), );



  // Override structure of built-in WordPress tags



  register_taxonomy( 'post_tag', 'post', array(

    'hierarchical'              => true, // Was false, now set to true

    'query_var'                 => 'tag', 'labels'                    => $labels, 'rewrite'                   => $rewrite, 'public'                    => true, 'show_ui'                   => true, 'show_admin_column'         => true, '_builtin'                  => true, ) );



}

add_action('init', 'wd_hierarchical_tags_register');

现在, 我已经继承了该项目, 我需要为标签(www.mywpsite.com/wp-json/wp/v2/tags)使用rest api端点, 但是我不断得到” rest_no_route / status: 404″响应, 就好像端点已禁用。有谁知道为什么吗?

无论如何, 谢谢你的时间。

最好的祝愿。

伊万


#1


解决了!我只需要在register_taxonomy函数的arguments数组内添加另外两个参数(” show_in_rest”和” rest_base”):

register_taxonomy( 'post_tag', 'post', array(
'hierarchical'              => true, // Was false, now set to true
'query_var'                 => 'tag', 'labels'                    => $labels, 'rewrite'                   => $rewrite, 'public'                    => true, 'show_ui'                   => true, 'show_admin_column'         => true, '_builtin'                  => true, 'show_in_rest'              => true, 'rest_base'                 => 'tags', 

));

赞(0)
未经允许不得转载:srcmini » WordPress:分层标签端点的问题

评论 抢沙发

评论前必须登录!