query_posts优化思路

时间:2019-02-02 18:08:42

标签: wordpress post

我在query_post中有一个分类法类别coupon_category,我试图用相同的coupon_category调用相关自定义分类法中的所有位置。

如果我使用:

<?php


                    // show all active coupons for this category from related store and setup pagination

                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        'tax_query' => array( 
                            array(
                            'taxonomy' => 'coupon_category', 
                            'field'    => 'slug',
                            'terms'    => 'mode', 
                            ),
    )

                    ) );

                ?>

我可以使用“模式”一词显示所有相关帖子,但是我想实现自动化,以便始终显示页面上已在使用的术语(coupon_category)。

1 个答案:

答案 0 :(得分:0)

找到了解决方案

$tax = get_the_terms($id, 'coupon_category');
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                query_posts( array(
                    'post_type' => APP_POST_TYPE,
                    'post_status' => 'publish',
                    'posts_per_page' => 4,
                    'tax_query' => array( 
                        array(
                        'taxonomy' => 'coupon_category',
                        'field'    => 'slug',
                        'terms'    => $tax[0]->slug,
                        ),
)

                ) );
相关问题