Wordpress - 允许在一个循环中多次显示相同的帖子

时间:2013-05-28 14:21:58

标签: wordpress loops posts

我有一系列Post ID

$ids = array(1277,6098,6709, 6098);

我想循环抛出这些特定帖子:

$args = array(   'orderby' => 'post__in',
                    'post__in' => $ids);

get_posts($args);
$custom_posts = get_posts($args);
foreach( $custom_posts as $post ) : setup_postdata($post);
    the_title();
    ...
endforeach;

但是wordpress会自动排除重复的Id(6098)。我怎么能避免这个?


我试着创建自己的功能。但不幸的是它不起作用。我创建了我自己的get_posts函数:

function get_posts_jt($args = null) {
                $defaults = array(
                    'numberposts' => 5, 'offset' => 0,
                    'category' => 0, 'orderby' => 'post_date',
                    'order' => 'DESC', 'include' => array(),
                    'exclude' => array(), 'meta_key' => '',
                    'meta_value' =>'', 'post_type' => 'post',
                    'suppress_filters' => true
                );

                $r = wp_parse_args( $args, $defaults );
                if ( empty( $r['post_status'] ) )
                    $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
                if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
                    $r['posts_per_page'] = $r['numberposts'];
                if ( ! empty($r['category']) )
                    $r['cat'] = $r['category'];
                if ( ! empty($r['include']) ) {
                    $incposts = $r['include'];
                    $r['posts_per_page'] = count($incposts);  // only the number of posts included
                    $r['post__in'] = $incposts;
                } elseif ( ! empty($r['exclude']) )
                    $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );

                $r['ignore_sticky_posts'] = true;
                $r['no_found_rows'] = true;

                $get_posts = new WP_Query;
                return $get_posts->query($r);

            }

我更改了以下行:

$incposts = wp_parse_id_list( $r['include'] );

为:

$incposts = $r['include'];

避免从数组中删除重复的ID。但是这个功能仍然没有显示我的Id列表中的重复帖子。

任何想法?

2 个答案:

答案 0 :(得分:2)

您可以循环播放ID并使用get_post

致电setup_postdata
global $post;
foreach ($ids as $id) :
    $post = get_post($id);
    setup_postdata( $post );
    the_title();
endforeach;

答案 1 :(得分:0)

您不能:get_posts使用wp_parse_id_list从提供的数组中删除重复的ID。