在子主题中添加时,注册的自定义分类术语不会显示

时间:2019-03-07 作者:vipworks

我已使用以下代码在我的子主题中注册了一个新的自定义帖子类型和taxonoy:

add_action(\'init\', \'tours_register\');
function tours_register() {
    $slug = \'tour\';
    $labels = array(
        \'name\' => $slug,
        \'singular_name\' => $slug,
        \'add_new\' => __(\'Add New\', \'themewaves\'),
        \'add_new_item\' => __(\'Add New Tour\', \'themewaves\'),
        \'edit_item\' => __(\'Edit Tour\', \'themewaves\'),
        \'new_item\' => __(\'New Tour\', \'themewaves\'),
        \'all_items\' => __(\'All Tours\', \'themewaves\'),
        \'view_item\' => __(\'View Tour\', \'themewaves\'),
        \'search_items\' => __(\'Search Tours\', \'themewaves\'),
        \'not_found\' =>  __(\'No Tour found\', \'themewaves\'),
        \'not_found_in_trash\' => __(\'No Tour found in Trash\', \'themewaves\'),
        \'menu_name\' => __(\'Tours\', \'themewaves\')
    );    
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => false,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'show_ui\' => true,
        \'hierarchical\' => false,

        \'menu_icon\' => THEME_DIR . \'/framework/assets/images/portfolio.png\',
        \'rewrite\' => array( \'slug\' => $slug),
        \'supports\' => array(\'title\', \'editor\',\'page-attributes\',\'thumbnail\',\'revisions\',\'comments\',\'custom-fields\')
    );
    register_post_type(\'tw_tour\', $args);
    flush_rewrite_rules();
}

function custom_tour_taxonomy(){
    $slug = \'tour\';
    register_taxonomy("cat_tour", array("tw_tour"), array("hierarchical" => true, "label" => __("Categories", "themewaves"), "singular_label" => __("Tour Category", "themewaves"), \'rewrite\' => array( \'slug\' => $slug.\'_cat\')));
}
add_action( \'after_setup_theme\', \'custom_tour_taxonomy\' );
之后,我必须在主题的页面生成器中创建一个新元素来显示此自定义帖子类型。在代码中的某个元素中,我添加了以下内容来获取类别:

//Tour Catigories
$tours = get_terms(array(
    \'taxonomy\' => \'cat_tour\',
    \'hide_empty\' => false,
));
$tour_categories = array("0" => "Select Category");
if(!empty($tours)) {
    foreach ($tours as $tour) {
        $tour_categories["$tour->term_id"] = $tour->name;
    }
}
然后我必须添加如下类别:

"recent_tours" => array(
        "name" => "Tour Carousel",
        "size" => "size-1-1",
        "only" => "builder",
        "settings" => array(
            "tour_category" => array(
                "title" => "Tour category",
                "type" => "category",
                "options" => $tour_categories,
                "default" => "0",
                "desc" => "Chosen categories will be included.",
            ));
但问题是$tour_categories 如果在子主题中添加CPT函数,则不会显示任何数据。如果我在主主题中添加相同的功能,则类别开始显示。但我只是想弄明白为什么它不能贯穿儿童主题。

非常感谢您在故障排除方面提供的任何帮助。提前谢谢!

1 个回复
SO网友:Jaydip Nimavat

注册分类更改:

add_action( \'after_setup_theme\', \'custom_tour_taxonomy\' );
收件人:

add_action( \'init\', \'custom_tour_taxonomy\' );
检查:https://codex.wordpress.org/Function_Reference/register_taxonomy#Basic_Example

相关推荐