我正在使用此代码获取我的帖子:
<?php
if ( get_query_var( \'paged\' ) ) {
$paged = absint( get_query_var( \'paged\' ) );
} elseif ( get_query_var( \'page\' ) ) {
$paged = absint( get_query_var( \'page\' ) );
} else {
$paged = 1;
}
$default_posts_per_page = get_option( \'posts_per_page\' );
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $default_posts_per_page,
\'paged\' => $paged,
);
$postslist = get_posts( $args );
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
$count = $wp_query->post_count;
?>
<?php foreach( $postslist as $post ) : setup_postdata( $post ); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<?php get_template_part( \'template-parts/content\', \'three-columns\' ); ?>
<?php endforeach; ?>
<?php if ( 0 === $count ) {
get_template_part( \'template-parts/content\', \'none\' );
} ?>
<?php
wp_reset_postdata(); ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
它可以毫无问题地获取我的帖子并显示出来,但只有一件小事需要处理。。。
我的贴子到处都找不到!(它们不会显示在首页上)
所以我的问题是:Is there a Wordpressy ( non-hacky way ) to get all the posts including the sticky ones?
基本上,我想让这段代码表现得像一个正常的循环(粘滞的帖子在前面,然后是正常的帖子,然后在时间上按照正确的时间顺序再次显示粘滞的帖子)。
因此,为了使我的代码像普通循环一样运行,我使用了以下黑客解决方法:
<?php
if ( get_query_var( \'paged\' ) ) {
$paged = absint( get_query_var( \'paged\' ) );
} elseif ( get_query_var( \'page\' ) ) {
$paged = absint( get_query_var( \'page\' ) );
} else {
$paged = 1;
}
$default_posts_per_page = get_option( \'posts_per_page\' );
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $default_posts_per_page,
\'include\' => implode(\',\', get_option(\'sticky_posts\')),
\'paged\' => $paged,
);
$args2 = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $default_posts_per_page,
\'paged\' => $paged,
);
$postslist = get_posts( $args );
$postslist2 = get_posts( $args2 );
$postslist = array_merge( $postslist, $postslist2 );
$postslist = array_map("unserialize", array_unique(array_map("serialize", $postslist)));
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
$count = $wp_query->post_count;
?>
<?php foreach( $postslist as $post ) : setup_postdata( $post ); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<?php get_template_part( \'template-parts/content\', \'three-columns\' ); ?>
<?php endforeach; ?>
<?php if ( 0 === $count ) {
get_template_part( \'template-parts/content\', \'none\' );
} ?>
<?php
wp_reset_postdata(); ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
此代码现在的行为类似于普通循环,但
it feels hacky 因为正如你所看到的,我正在使用
get_posts()
两次(绝对多余)然后我使用
array_merge()
合并两个
get_posts()
合为一体
array
最后,我通过
array_map()
和
array_unique()
.
因此,我将重复这个问题:Is there a better ( Wordpressy/non-hacky ) way to get all the posts including the sticky ones with foreach loop?
谢谢大家!!
SO网友:rocknrollcanneverdie
更新答案:
WP的默认行为正是您想要的。问题是您正在使用get_posts()
通过强制ignore_sticky_posts = true
(check the source). 所以您只需要一个查询,只需使用WP\\u query():
$postsList = new WP_Query([
\'post_type\' => \'post\',
\'posts_per_page\' => $default_posts_per_page,
\'paged\' => $paged
]);
粘性贴子将位于顶部,如果分页,将在按日期排序的页面上再次重复(默认排序)。
为了避免帖子的粘性,请使用\'ignore_sticky_posts\' => true
在您的查询中。它不排除粘性帖子,只是删除了“粘性行为”,所以帖子将按日期排序(默认排序)。
new WP_Query([
\'post_type\' => \'post\',
\'posts_per_page\' => $default_posts_per_page,
\'paged\' => $paged,
\'ignore_sticky_posts\' => true
]);
Codex链接:
https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters