簡體   English   中英

在wordpress中過濾子類別

[英]Filter Sub category in 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); 

幫助我只顯示主要類別!

你應該在args數組中為父類(父類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文檔鏈接的底部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM