wordpress自定义帖子类型循环按标记ID

时间:2016-11-16 16:24:24

标签: wordpress wordpress-theming custom-wordpress-pages

我正在尝试按标记ID制作自定义类型的帖子循环 这是原始代码:

<?php $args = array(
            'post_type' => 'kana_portfolio',
            'meta_key'   => 'choose_layout_2',
            'meta_value' => 'layout-1',
            'posts_per_page' => -1
        );
        $posts = get_posts($args);
        $term_array = array();
        $portfolio_term_array = array();
        foreach ( $posts as $post ) : setup_postdata( $post );
            $terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
            foreach($terms as $term){
                $term_array[$term->slug] = $term->name;
                $portfolio_term_array[] = $term->slug;
            }
        endforeach;
        wp_reset_postdata(); ?>

由于我只需要循环ID为31的特定类别(自定义帖子类型)中的帖子,因此在“'posts_per_page'=&gt; -1”之后添加了“'tag_id'=&gt; 31”

<?php $args = array(
            'post_type' => 'kana_portfolio',
            'meta_key'   => 'choose_layout_2',
            'meta_value' => 'layout-1',
            'posts_per_page' => -1,
            'tag_id' => 31
        );
        $posts = get_posts($args);
        $term_array = array();
        $portfolio_term_array = array();
        foreach ( $posts as $post ) : setup_postdata( $post );
            $terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
            foreach($terms as $term){
                $term_array[$term->slug] = $term->name;
                $portfolio_term_array[] = $term->slug;
            }
        endforeach;
        wp_reset_postdata(); ?>

[编辑]
我在页面下面有另一个循环,我试图插入'cat'=&gt; 31 tax_query数组

                <?php $number_posts_to_display = get_field('number_of_posts_to_display');
                $display_order = get_field('post_display_order');
                $portfolio = array(
                    'post_type' => 'kana_portfolio',
                    'posts_per_page' => $number_posts_to_display,
                    'order' => $display_order,
                    'meta_key'   => 'choose_layout_2',
                    'meta_value' => 'layout-1',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'kana_genre',
                            'field'    => 'slug',
                            'terms'    => $portfolio_term_array,
                        ),
                    ),
                );
                $portfolio_loop = new WP_Query($portfolio); ?>

但没有显示,我该怎么办?

2 个答案:

答案 0 :(得分:0)

如果你的类别(我们在这里说的是分类型类别的真实类别......)是31,你应该使用&#39; cat&#39; =&gt; 31,标签是标签,它们不是类别..如果这有帮助,请告诉我。如果你试图对标签进行过滤并且它无法正常工作,那么请让我知道并深入了解。

玩得开心!

答案 1 :(得分:0)

而不是:

globals()

试试这个:

'tag_id' => 31