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