简体   繁体   English

在wordpress中过滤子类别

[英]Filter Sub category in wordpress

Hi I want to display only parent category but this code shows subcategory too. 嗨我想只显示父类别,但此代码也显示子类别。 am not able to remove subcategory from this . 我无法从中删除子类别。

and one more thing how I can show only one parent category with subcategory.. 还有一件事我怎么只能用子类别显示一个父类别..

    <?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'
    );
?>

and this 和这个

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

and output via this 并通过这个输出

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

help me out to show only main category ! 帮助我只显示主要类别!

you should pass parent (parent 0 for main categories) parameter also in args array for get_categories like below. 你应该在args数组中为父类(父类0为主要类别)参数传递get_categories,如下所示。

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

$filter_options = get_categories( $args );

it will get only parent categories, further you can check at bottom of following wordpress documentation link in "Get only top level categories" section https://developer.wordpress.org/reference/functions/get_categories/ 它只会获得父类别,您可以在“仅获取顶级类别”部分https://developer.wordpress.org/reference/functions/get_categories/中查看以下wordpress文档链接的底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM