Wordpress-允许在一个循环中多次显示相同的帖子
我得到了一组帖子IDWordpress-允许在一个循环中多次显示相同的帖子,wordpress,loops,posts,Wordpress,Loops,Posts,我得到了一组帖子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
$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'] );
致:
避免从阵列中删除重复的ID。但是这个函数仍然不会显示Id列表中的重复帖子
有什么想法吗?您不能:
get\u posts
用于从提供的数组中删除重复的ID。您可以循环ID,并使用设置\u postdata
调用get\u post
:
global $post;
foreach ($ids as $id) :
$post = get_post($id);
setup_postdata( $post );
the_title();
endforeach;
你可以滚动你自己的函数(例如,
my_get_posts
和my_wp\u parse\u id_list
)这是一篇老文章,但这是解决这个问题的方法,不知道为什么我没有想到:)Thx
global $post;
foreach ($ids as $id) :
$post = get_post($id);
setup_postdata( $post );
the_title();
endforeach;