admin管理员组文章数量:1417661
I'm currently working on displaying recent posts in 'masonry grid'. Because of multiple issues I came out with the idea of displaying pre-defined rows of that grid (due to designed layout).
First I did all that without dynamic content (posts). Then I tried to get WP_Query to work. But instead of displaying multiple posts in one row, I'm displaying one multiplied post per each row.
I already did "more posts" button - and it reveals content as described above too.
I suppose that prev_post()
method may be wrong for that case, but I didn't found in Codex anything that might be better.
Below I attach part of code & screens to better show both problem and desired effect.
$wpb_all_query = new WP_Query([
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '4',
]);
if ($wpb_all_query->have_posts()) {
while ($wpb_all_query->have_posts()) {
ob_start();
?>
<div class="list_row three_col">
<div class="column column_1_3">
<?php
$wpb_all_query->the_post(); //1 - the newest post
get_template_part('partials/list-single-post-masonry');
?>
</div>
<div class="column column_1_3">
<?php
$wpb_all_query->prev_post(); //2 - older older than 1
get_template_part('partials/list-single-post-masonry');
$wpb_all_query->prev_post(); //3 - older than 2
get_template_part('partials/list-single-post-masonry');
?>
</div>
<div class="column column_1_3">
<?php
$wpb_all_query->prev_post(); //4 - older than 3
get_template_part('partials/list-single-post-masonry');
?>
</div>
</div>
<?php
$generated_content = ob_get_contents();
ob_end_clean();
wp_reset_postdata();
}
}
return $generated_content;
SCREEN no.1 - how it's looking now
SCREEN no.2 - how it should look like
I'm currently working on displaying recent posts in 'masonry grid'. Because of multiple issues I came out with the idea of displaying pre-defined rows of that grid (due to designed layout).
First I did all that without dynamic content (posts). Then I tried to get WP_Query to work. But instead of displaying multiple posts in one row, I'm displaying one multiplied post per each row.
I already did "more posts" button - and it reveals content as described above too.
I suppose that prev_post()
method may be wrong for that case, but I didn't found in Codex anything that might be better.
Below I attach part of code & screens to better show both problem and desired effect.
$wpb_all_query = new WP_Query([
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '4',
]);
if ($wpb_all_query->have_posts()) {
while ($wpb_all_query->have_posts()) {
ob_start();
?>
<div class="list_row three_col">
<div class="column column_1_3">
<?php
$wpb_all_query->the_post(); //1 - the newest post
get_template_part('partials/list-single-post-masonry');
?>
</div>
<div class="column column_1_3">
<?php
$wpb_all_query->prev_post(); //2 - older older than 1
get_template_part('partials/list-single-post-masonry');
$wpb_all_query->prev_post(); //3 - older than 2
get_template_part('partials/list-single-post-masonry');
?>
</div>
<div class="column column_1_3">
<?php
$wpb_all_query->prev_post(); //4 - older than 3
get_template_part('partials/list-single-post-masonry');
?>
</div>
</div>
<?php
$generated_content = ob_get_contents();
ob_end_clean();
wp_reset_postdata();
}
}
return $generated_content;
SCREEN no.1 - how it's looking now
SCREEN no.2 - how it should look like
Share Improve this question asked Aug 1, 2019 at 9:09 BajecznyBajeczny 52 bronze badges1 Answer
Reset to default 0Change $wpb_all_query->prev_post()
to $wpb_all_query->the_post()
.
Class WP_Query
does not have prev_post()
method, it has the next_post()
method, which takes the next post from the results (set up post
member of WP_Query object), but it does not set the global variable. To reach for the data of the current post, you must use $wpb_all_query->post
(eg. $wpb_all_query->post->post_name ) or use $wpb_all_query->setup_postdata()
to set the global variable $post
.
$wpb_all_query->the_post()
is equivalent to:
$wpb_all_query->next_post();
$wpb_all_query->setup_postdata();
本文标签: phpGetting info about selected posts using one WPQuery
版权声明:本文标题:php - Getting info about selected posts using one WP_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745279070a2651336.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论