我试图在每个第n个常规帖子后反复在主帖子循环中拉出一个自定义帖子类型。它应该是这样的:
post1 post2 post3
post4 post5 post6
---------cpt1----------
post7 post8 post9
post10 post11 post12
---------cpt2---------- ...and so forth
我的代码:
<?php
// Initialize counters for the number of posts and to count the custom post type to set the offset
$post_counter = 0;
$collection_offset = 0;
// Initialization for the main query
$post_args = array(
'post_type'=>'post',
'posts_per_page' => 12
);
$post_query = new WP_Query($post_args);
// Start main loop to load only posts of the type 'post'
while ($post_query->have_posts()) : $post_query->the_post();
// Load template file
get_template_part( 'template-parts/content', get_post_format() );
// backup the current $post
$bckp_post = $post;
global $post;
// Prepare inner loop only after every 6 posts
if($post_counter != 0 && $post_counter % 5 == 0) :
// initialization for inner query. Only one cpt should be pulled every time, with increasing offset
$collection_args = array(
'post_type' => 'nls_collection',
'post_per_page' => 1,
'offset' => $collection_offset
);
$collection_query = new WP_Query($collection_args);
// Start inner loop
while ($collection_query->have_posts()) : $collection_query->the_post();
// Load the title ?>
</div>
<div class="row">
<div class="col s12 red">
<?php echo the_title(); ?>
</div>
</div>
<div class="row">
<?php $collection_offset++;
endwhile;
endif;
// restore the global $post from the previously created backup and increment post counter
$post=$backup;
wp_reset_query();
$post_counter++;
endwhile;
现在,两个cpts都是相互打印的。我陷入了困境。有人有线索吗?
更新的代码:
<main id="main" class="site-main grey lighten-4" role="main">
<p class="flow-text center grey lighten-4">Neueste Beiträge</p>
<div id="primary" class="container grey lighten-4 feed-main">
<div class="row">
<?php
// Initialize counters for the number of posts
$post_counter = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Initialization for the main query and the query for the inner loop
$post_args = array(
'post_type'=>'post',
'posts_per_page' => 12,
'paged' => $paged
);
$post_query = new WP_Query($post_args);
$collection_args = array(
'post_type' => 'nls_collection',
'posts_per_page' => 2,
'paged' => $paged
);
$collection_query = new WP_Query($collection_args);
// Start main loop to load only posts of the type 'post'
while ($post_query->have_posts()) : $post_query->the_post();
// Load template file
get_template_part('template-parts/content');
// backup the current $post
$backup_post = $post;
global $post;
// Prepare inner loop only after every 6 posts
if($post_counter != 0 && $post_counter % 6 == 0) :
if($inner_backup): $post = $inner_backup; endif;
// Start inner loop
if($collection_query->have_posts()) : $collection_query->the_post();
// Load the title ?>
</div>
<div class="row">
<div class="col s12 red">
<h1><?php echo the_title(); ?></h1>
</div>
</div>
<div class="row">
<?php
//$inner_backup = $post;
//global $post;
endif;
endif;
// restore the global $post from the previously created backup and increment post counter
$post = $backup_post;
wp_reset_query();
$post_counter++;
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'nlscustom' ),
'next_text' => __( 'Next page', 'nlscustomn' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'nlscustom' ) . ' </span>',
) );
?>
</div>
答案 0 :(得分:1)
辅助查询的while循环导致两个查询的自定义帖子一起显示。
我还建议在进入循环之前准备两组查询数据。类似下面的代码(未经测试)应该按照你的描述进行:
<?php
# Set post counter and pagination
$post_counter = 0;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
# Prepare main query
$args = array(
'post_type'=>'post',
'posts_per_page' => 12,
'paged' => $paged,
);
$main_query = new WP_Query($post_args);
# Prepare custom post query
$args = array(
'post_type' => 'nls_collection',
'post_per_page' => 2,
'paged' => $paged,
);
$secondary_query = new WP_Query($args);
# Start main loop to load only posts of the type 'post'
while ($post_query->have_posts()) : $post_query->the_post();
# Output post from main query
#
#
$post_counter++;
# Prepare inner loop only after every 6 posts
if($post_counter % 5 == 0) :
# No while statement needed here. You'll do these one at a time.
# the_post() method automatically advances the pointer on your query.
$secondary_query->the_post();
# Output post from secondary query
#
#
endif;
endwhile;