2016-06-08 2 views
1

안녕하세요, 상위 카테고리 만 표시하고 싶지만이 코드는 하위 카테고리도 표시하고 있습니다. 하위 카테고리를 제거 할 수 없습니다.WordPress의 하위 범주

내가 하위 범주와 하나의 상위 범주를 표시 할 수있는 방법 한 가지 더 ..

<?php 

class AQ_Portfolio_Block extends AQ_Block { 

//set and create block 
function __construct() { 
    $block_options = array(
     'name' => 'Portfolio', 
     'size' => 'span12', 
     'resizable' => 0, 
     'block_description' => 'Add a feed of Portfolio posts to the page.', 
     'block_category' => 'feeds', 
     'block_icon' => '<i class="fa fa-fw fa-paint-brush"></i>' 
    ); 
    parent::__construct('aq_portfolio_block', $block_options); 
}//end construct 

function form($instance) { 
    $defaults = array(
     'type' => 'classic', 
     'pppage' => '999', 
     'filter' => 'all', 
     'show_filter' => 1 
    ); 

    $instance = wp_parse_args($instance, $defaults); 
    extract($instance); 

    $args = array(
     'orderby'     => 'name', 
     'hide_empty'    => 0, 
     'hierarchical'    => 1, 
     'taxonomy'     => 'portfolio_category' 
    ); 

    $filter_options = get_categories($args); 

    $portfolio_types = array(
     'classic' => 'Classic Masonry', 
     'masonry' => 'Fixed Masonry', 
     'full' => 'Full Width', 
     'classic-lightbox' => 'Classic Masonry Lightbox', 
     'masonry-lightbox' => 'Fixed Masonry Lightbox', 
     'full-lightbox' => 'Full Width Lightbox' 
    ); 
?> 

아웃이

if(1 == $show_filter) 
       echo ebor_portfolio_filters($cats); 

도움이 나를 통해이

<?php 
}//end form 

function block($instance) { 
    extract($instance); 

    /** 
    * Initial query args 
    */ 
    $query_args = array(
     'post_type' => 'portfolio', 
     'posts_per_page' => $pppage 
    ); 

    /** 
    * If we're choosing just 1 category, add more args. 
    * GRAB ALL THE ARGS! 
    */ 
    if (!($filter == 'all')) { 
     if(function_exists('icl_object_id')){ 
      $filter = (int)icl_object_id($filter, 'portfolio_category', true); 
     } 
     $query_args['tax_query'] = array(
      array(
       'taxonomy' => 'portfolio_category', 
       'field' => 'id', 
       'terms' => $filter 
      ) 
     ); 
    } 

    /** 
    * Finally, here's the query. 
    */ 
    $block_query = new WP_Query($query_args); 

    /** 
    * Now let's grab categories for the animated portfolio filter buttons 
    */ 
    $cats = ($filter == 'all') ? get_categories('taxonomy=portfolio_category') : get_categories('taxonomy=portfolio_category&exclude='. $filter .'&child_of='. $filter); 

    if('classic' == $type) : 
?> 

및 출력 메인 카테고리 만 표시!

답변

0

다음과 같이 get_categories의 args 배열에도 parent (주 카테고리의 부모 0) 매개 변수를 전달해야합니다.

$args = array(
    'orderby'     => 'name', 
    'hide_empty'    => 0, 
    'hierarchical'    => 1, 
    'taxonomy'     => 'portfolio_category', 
    'parent'     => 0 
); 

$filter_options = get_categories($args); 

이 더 당신이> 0은 여전히 ​​모든 범주를 보여 = '부모'를하고있는 부분 https://developer.wordpress.org/reference/functions/get_categories/

+0

"에만 최상위 범주 가져 오기"에서 워드 프레스 문서 링크를 다음의 하단에 확인하실 수 있습니다 만 상위 범주를 얻을 것이다 –

+0

get_cms 대신 get_terms 함수를 사용해 볼 수 있습니까? –

+0

parent => 0이 여전히 작동하지 않으면 parent => 0 대신 childless => true 매개 변수를 사용하여 childless의 자세한 내용을 확인하고 'childless'라는이 링크를 확인하십시오. (bool) 결과를 true로 제한하려면 True로 설정하십시오. 자녀가없는 용어. 이 매개 변수는 비 계층 적 분류에 영향을 미치지 않습니다. 기본 false.https : //developer.wordpress.org/reference/functions/get_terms/. –