显示来自已确定父级的单个帖子子类别

时间:2021-02-27 作者:Stuart66

我正在尝试在选定类别的单个post-child类别中显示。对于某些单个帖子,我有一个对应于某个区域的类别。我想在我的单个帖子模板的一个特殊区域显示该类别。

我试过几种方法,但都做不到。到目前为止,我又回到了最初的岗位类别:

$regions = get_the_category( array(
        \'parent\' => \'62\',
    ));
有人能帮我吗?非常感谢。

1 个回复
最合适的回答,由SO网友:Stuart66 整理而成

最后,我使用了自定义分类法,它也更易于管理。我发布了以下内容:

//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
 
add_action( \'init\', \'create_regions_nonhierarchical_taxonomy\', 0 );
 
function create_regions_nonhierarchical_taxonomy() {
 
// Labels part for the GUI
 
  $labels = array(
    \'name\' => _x( \'Regions\', \'Where are located activities or place of interests\' ),
    \'singular_name\' => _x( \'Region\', \'Regions singular name\' ),
    \'search_items\' =>  __( \'Search Regions\' ),
    \'popular_items\' => __( \'Popular Regions\' ),
    \'all_items\' => __( \'All Regions\' ),
    \'parent_item\' => null,
    \'parent_item_colon\' => null,
    \'edit_item\' => __( \'Edit Region\' ), 
    \'update_item\' => __( \'Update Region\' ),
    \'add_new_item\' => __( \'Add New Region\' ),
    \'new_item_name\' => __( \'New Region Name\' ),
    \'separate_items_with_commas\' => __( \'Separate regions with commas\' ),
    \'add_or_remove_items\' => __( \'Add or remove regions\' ),
    \'choose_from_most_used\' => __( \'Choose from the most used regions\' ),
    \'menu_name\' => __( \'Regions\' ),
  ); 
 
// Now register the non-hierarchical taxonomy like tag
 
  register_taxonomy(\'regions\',\'post\',array(
    \'hierarchical\' => false,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_in_rest\' => true,
    \'show_admin_column\' => true,
    \'update_count_callback\' => \'_update_post_term_count\',
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'region\', \'with_front\' => false),
  ));
}