在wordpress中过滤子类别

时间:2016-06-08 06:53:19

标签: php 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'
    );
?>

和这个

<?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 ) :
?>

并通过此

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

帮助我显示主要类别!

1 个答案:

答案 0 :(得分:0)

你应该在args数组中传递parent(主类别的父0)参数,如下面的get_categories。

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

$filter_options = get_categories( $args );

它只会获得父类别,您可以在“仅获取顶级类别”部分https://developer.wordpress.org/reference/functions/get_categories/

中查看以下wordpress文档链接的底部