是否可以从某个post ID开始循环?我希望所有的帖子都从某个ID开始,体面地。为了解释,我必须先做出一些假设。
假设我的WordPress数据库有20篇文章。显然,每个帖子都有一个ID,并假设这些帖子的ID范围为1到20。
还假设当我们通过循环加载post时,默认情况下使用的是所有内容。因此,循环将从20到1检索记录/帖子。如果范围限制为10,则第1页将检索20到11,第2页将检索10到1。
现在,回到我的问题上来。假设我想接收记录17到8。我是否可以通过某种方式将17传递给循环,并从17到8检索10条记录?这可能吗?
以下是有助于理解问题的几行代码:
<?php
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => \'news\',
\'posts_per_page\' => 10,
\'ID_VARIABLE_HERE\' => 17 // I would like to be able to pass the ID, and the loop should return 10 posts (as defined in the posts_per_page), descendingly, starting at 17.
);
$custom_query = new WP_Query($args);
?>
<?php if ($custom_query->have_posts()): while ($custom_query->have_posts()) : $custom_query->the_post() ; ?>
<article class="my-article">
<div class="my-image"><?php the_post_thumbnail(); ?></div>
<div class="my-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<div class="my-details"><?php the_excerpt(); ?></div>
</article>
<?php endwhile; else: ?>
<p><?php _e(\'No contents exist.\'); ?></p>
<?php endif; ?>
正如你所看到的,我想通过一个论点
$args
, 将具有ID值(上例中为17)。阅读上面的代码注释,了解所需的详细信息。有可能吗?如果不是通过中的参数
$args
, 另一种方法是什么?
谢谢
SO网友:ghoul
您可以执行以下操作,提供要以$max\\u post\\u id开头的post id,并向下递减到可以以$min\\u post\\u id提供的特定post\\u id:
// Get all post ids.
$all_post_ids = get_posts(array(
\'fields\' => \'ids\', // Only get post IDs
\'posts_per_page\' => -1
));
// Make a list of all possible post ids between two post ids.
$max_post_id = 17;
$min_post_id = 8;
$custom_post_id = [];
for ( $i = $max_post_id; $i > $min_post_id; $i-- ) {
$custom_post_id[] = $i;
}
// Filter out the valid post ids from the possible post ids.
$query_post_ids = array_intersect( $custom_post_id, $all_post_ids );
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => \'news\',
\'posts_per_page\' => 10,
\'post__in\' => $query_post_ids // Use the valid post ids in the array.
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ):
while ($custom_query->have_posts()) :
$custom_query->the_post() ; ?>
<article class="my-article">
<div class="my-image"><?php the_post_thumbnail(); ?></div>
<div class="my-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<div class="my-details"><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
else: ?>
<p><?php _e(\'No contents exist.\'); ?></p>
<?php endif;
请注意,这可能会产生性能问题,因为如果max和min之间的差距太大,我们将使用“for”循环创建递减为1的可能id。
SO网友:Elex
有一种方法pre_get_posts
和WP_Query
使用自定义参数!:)
假设这是我们的$args
.
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => \'news\',
\'posts_per_page\' => 10,
\'maximum_id\' => 17, // I choose this name for example, feel free
);
你必须抓住
maximum_id
中的参数
pre_get_posts
钩
function wpse_136861_pre_get_posts($query) {
if(intval($query->get(\'maximum_id\')) > 0 && $query->get(\'maximum_id\') >= $query->get(\'posts_per_page\'))
{
$start = $query->get(\'maximum_id\') - $query->get(\'posts_per_page\');
// Be sure to avoid the negative / 0 value
if($start <= 0)
{
$start = 1;
}
$end = $query->get(\'maximum_id\');
$query->set(\'post__in\', range($start, $end)); // Set your post_id array with all int beetween $start and $end (7-17 here)
$query->set(\'orderby\', \'ID\'); // Set by ID DESC (DESC order is default in WP_Query)
}
return $query;
}
add_action(\'pre_get_posts\', \'wpse_136861_pre_get_posts\');
这项工作:)