0
我想在每个第n个常规帖子之后重复在主帖子循环中只重复一个自定义帖子类型。它应该是这样的:在每个第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;
眼下,双方都深圳华映后,对方打印。我被困在这一点上。有人有线索吗?
更新代码:
<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>
按预期工作!非常感谢!但现在还有另一个问题:下一页加载时,有相同的帖子...新页面不加载新帖子。我必须调整分页或什么吗? – gervik88
太好了。在查询参数中包含''paged''参数应该可以做到。看看上面编辑的代码,看看我的意思。 – lalala
感谢您的支持@lalala。不幸的是,它不能正常工作:在下一页没有自定义的帖子类型,并且帖子不再被链接(在他们的第一页上)。我更新了上面的代码。我正在使用无限滚动插件(http://wordpress-ajax-pagination.com/),这可能是分页问题的原因吗? – gervik88