Wordpress 在每第n个post之后,在主post循环中拖动一个自定义post类型

Wordpress 在每第n个post之后,在主post循环中拖动一个自定义post类型,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我试图在每第n个常规帖子之后,在主帖子循环中反复拉一个自定义帖子类型。应该是这样的: post1 post2 post3 post4 post5 post6 ---------cpt1---------- post7 post8 post9 post10 post11 post12 ---------cpt2---------- ...and so forth 我的代码: <?php // Initialize count

我试图在每第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;
现在,两个CPT都是依次打印的。我被困在这一点上了。有人有线索吗

更新代码:

<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>

辅助查询上的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;

一切如愿!非常感谢你!但现在还有另一个问题:当加载下一页时,会出现相同的帖子……新页面不会加载新帖子。我需要调整页码还是什么?太好了。在查询参数中包含“paged”参数应该可以做到这一点。看看上面编辑的代码,看看我的意思。谢谢你对@lalala的支持。不幸的是,它不能正常工作:在下一页上没有自定义的文章类型,并且文章在第一页上不再链接。我更新了上面的代码。我正在使用一个无限滚动的插件,这可能是分页问题的原因吗?肯定是@gervik88。这就是你应该看的地方。我已经解决了这个问题,最后…稍后将发布代码。谢谢你的Tipps@lalala