【问题标题】:Dropdown categories wordpress下拉类别 wordpress
【发布时间】:2015-10-17 17:04:39
【问题描述】:

我在搜索表单中使用下拉类别功能。当我使用选项“全部”时,它不会显示任何结果,我希望显示所有类别的所有帖子。我错过了什么?

以下是我的查询:

if ( isset($_GET['cat']) && isset($_GET['manufacturer']) ) {

                    $tax_query = array( 'relation' => 'AND' );
                    array_push($tax_query,
                        array(
                            'taxonomy'  => 'manufacturers',
                            'field'     => 'id',
                            'terms'     => $_GET['manufacturer']
                        ),
                        array(
                            'taxonomy'  => 'category',
                            'field'     => 'id',
                            'terms'     => $_GET['cat']
                        )
                    );
                }
$query_args = array(
                    'post_type'         => 'yacht',
                    'meta_or_tax'       => true,
                    'tax_query'         => $tax_query,
                    'posts_per_page'    => -1,
                    'meta_query'        => array(
                        'relation'      => 'AND',
                        array(
                         'key'          => 'yachts_loa_length_round',
                         'value'        => array($_GET['min_length'], $_GET['max_length']),
                         'compare'      => 'BETWEEN',
                         'type'         => 'NUMERIC',
                        ),
                        array(
                         'key'          => 'yachts_price',
                         'value'        => array($_GET['min_price'], $_GET['max_price']),
                         'compare'      => 'BETWEEN',
                         'type'         => 'NUMERIC',
                        ),
                        array(
                         'key'          => 'yachts_year',
                         'value'        => array($_GET['min_year'], $_GET['max_year']),
                         'compare'      => 'BETWEEN',
                         'type'         => 'NUMERIC',
                        )
                    )
                );

                $yacht_query = new WP_Query( $query_args );

和我的 php 函数:

$args = array(

                'orderby'            => 'menu_order',
                'show_option_all'   => pll__('All '),
                'order'              => 'ASC',
                'hide_empty'         => 0,
                'class'              => 'form-select',
                'taxonomy'           => 'category',
                'value_field'        => 'term_id'
              );

              wp_dropdown_categories( $args ); ?>

 $args = array(

                'orderby'            => 'menu_order',
                'show_option_all'   => pll__('All'),
                'order'              => 'ASC',
                'hide_empty'         => 0,
                'name'               => 'manufacturer',
                'class'              => 'form-select',
                'taxonomy'           => 'manufacturers',
                'value_field'        => 'term_id'
              );

              wp_dropdown_categories( $args ); ?>

【问题讨论】:

  • 用户 wp_list_categories 并使其像这样 <?php wp_list_categories('hide_empty=0&show_count=1&title_li=<h2>Categories</h2>'); ?>
  • 这不会创建我需要的下拉菜单并且没有帮助。对不起

标签: php wordpress categories


【解决方案1】:

在 wordpress.stackexchange.com 上 @Milo 的帮助下,设法解决了这个问题

$tax_query = '';

if( isset($_GET['cat']) && 0 != $_GET['cat'] ) {
    $tax_query[] = array(
        'taxonomy'  => 'category',
        'field'     => 'term_id',
        'terms'     => $_GET['cat']
    );
}

if( isset($_GET['manufacturer']) && 0 != $_GET['manufacturer'] ) {
    $tax_query[] = array(
        'taxonomy'  => 'manufacturers',
        'field'     => 'term_id',
        'terms'     => $_GET['manufacturers']
    );
}

// only add relation if both are set and non-zero
if( isset($_GET['manufacturer']) && 0 != $_GET['manufacturer'] && isset($_GET['cat']) && 0 != $_GET['cat'] ) {                
    $tax_query['relation'] = 'AND';
}

// query args for all queries
$query_args = array(
    'post_type' => 'yacht',
    // other args...
);

// add tax query if it isn't empty
if( !empty( $tax_query ) ){
    $query_args['tax_query'] = $tax_query;
}

$yacht_query = new WP_Query( $query_args );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多