如何在子类别Genesis Wordpress中专门添加小部件

时间:2018-03-13 05:55:32

标签: php html wordpress genesis

基本上我有这些类别层次

  • / service [parent] [id - > 3]
    • 帖子
    • / service / type [sub] [id - > 11]
      • / service / type / outsourcing [sub]
    • / service / problem [sub] [id - > 12]
      • / service / type / outsourcing [sub]

我有一个小部件,据说只是目标/服务/类型和/服务/问题,但问题是小部件也添加到/ service下的帖子。

以下是在functions.php中创建小部件的代码



genesis_register_sidebar( array(
'id' => 'service-type-widget',
'name' => __( 'Service Type Page', 'genesis' ),
'description' => __( 'For service page', 'childtheme' ),
) );
add_action( 'genesis_after_content', 'service_type_widget' );
function service_type_widget() {
    if ( in_category('3')) {
        genesis_widget_area( 'service-type-widget', array(
        'before' => '<div class="widget--service-type widget--service widget-area">',
        'after'  => '</div>',
        ) );
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我通过使用@bainternet回答的is_subcategory()解决了这个问题 https://wordpress.stackexchange.com/questions/27982/equivalent-to-in-category-for-subcategories

function is_subcategory($category = null) {
    if (is_category()) {
        if (null != $category){
            $cat = get_category($category);
        }else{
            $cat = get_category(get_query_var('cat'),false);
        }
        if ($cat->parent == 0 ){
            return false;
        }else{
            return true;
        }
    }
    return false;
}

genesis_register_sidebar( array(
'id' => 'service-type-widget',
'name' => __( 'Service Type Page', 'genesis' ),
'description' => __( 'For service page', 'childtheme' ),
) );
add_action( 'genesis_after_content', 'service_type_widget' );
function service_type_widget() {
    if ( is_subcategory('11')) {
        $child_cats = get_categories('&child_of=3');
        genesis_widget_area( 'service-type-widget', array(
        'before' => '<div class="widget--service-type widget--service widget-area">',
        'after'  => '</div>',
        ) );
    }
}