簡體   English   中英

我該如何插入 <div> 在無限加載的WordPress循環中每5個帖子之后?

[英]How do I insert a <div> after every 5 posts in a WordPress Loop that infinitely loads?

因此,我設置了一個WPQuery循環,該循環很好用,並使用無限滾動功能來保持每10個帖子的批次加載。 我想做的是在列表中每第5個帖子后插入一個<div> 我向它添加了一個反代碼,但它似乎沒有給我想要的輸出。 這是代碼:

<?php
   $featuredPosts = get_field('featured_posts');
   $excludePosts = [];
   foreach($featuredPosts as $key => $postItem) {
      $excludePosts[] = $postItem->ID;
   }

  $numPosts = 10;
  $args = array(
    'post_type'  => 'post',
    'post_status' => 'publish',
    'posts_per_page' => $numPosts,
    'post__not_in' => $excludePosts
  );


  $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
      $counter = 0;
      while ( $the_query->have_posts() ) {
        $the_query->the_post();
?>
        <article id="post-<?php echo get_the_ID(); ?>" <?php post_class('frontpage-articles'); ?> >
            <div class="entry-container">
                <div class="post-meta"><span class="entry-date"><?php echo get_the_date("M d, Y", get_the_ID()); ?></span>
                    <span class="entry-author">by
                       <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))) ?>"><?php the_author(); ?></a>
                    </span>
                </div>
                <header class="entry-header">
                    <h3 class="entry-title">
                        <?php echo sanitize_title(the_title( '<a href="' . esc_url(get_permalink($post->ID) ) . '">', '</a>' )); ?></h3>
                </header><!-- .entry-header -->

                <div class="entry-summary">
                    <?php /*echo substr(strip_tags(get_the_excerpt()), 0,999); */?>
                    <?php echo substr(strip_tags(get_the_excerpt()), 0, 120); ?>
                </div><!-- .entry-summary -->
            </div>
            <!-- #thumbnail-->
            <?php
            $cloudinaryImage = get_post_meta($post->ID, 'cloudinary_image_id');
            if ( has_post_thumbnail() ) {
                $thumbnailUrl = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
            }
            ?>

            <?php
            $thumbnail = '';
            if(!empty($cloudinaryImage[0])) {
                $thumbnail = "https://res.cloudinary.com/upload/".$cloudinaryImage[0];
            } else if(!empty($thumbnailUrl)) {
                $thumbnail = $thumbnailUrl[0];
            } ?>
            <a href="<?php echo esc_url(get_permalink($post->ID))?> ">
                <div class="entry-thumb" style="background-image: url('<?= $thumbnail; ?>'); background-repeat: no-repeat; background-size: cover;" alt="">
                </div>
            </a>
            <!-- .thumbnail-image -->
        </article>
            <?php 
                if ($counter % 5 == 0){
                  echo '<div>Ads Test Div to Be inserted after every 5th post</div>';
                }
                $counter++;
            ?>
        <hr>
        <?php

    }
    /* Restore original Post Data */
    wp_reset_postdata();
}

我看不出計數有什么問題嗎? 我相信它在正確的位置。 任何幫助將不勝感激。

您的$ count ++++; 在if($ counter%5 == 0)之后。 您應該移動$ counter ++;。 否則,如果$ counter在除法之前需要增加價值。

試試這個代碼:

<?php
   $featuredPosts = get_field('featured_posts');
   $excludePosts = [];
   foreach($featuredPosts as $key => $postItem) {
      $excludePosts[] = $postItem->ID;
   }

  $numPosts = 10;
  $args = array(
    'post_type'  => 'post',
    'post_status' => 'publish',
    'posts_per_page' => $numPosts,
    'post__not_in' => $excludePosts
  );


  $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
      $counter = 0;
      while ( $the_query->have_posts() ) {
        $the_query->the_post();
?>
        <article id="post-<?php echo get_the_ID(); ?>" <?php post_class('frontpage-articles'); ?> >
            <div class="entry-container">
                <div class="post-meta"><span class="entry-date"><?php echo get_the_date("M d, Y", get_the_ID()); ?></span>
                    <span class="entry-author">by
                       <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))) ?>"><?php the_author(); ?></a>
                    </span>
                </div>
                <header class="entry-header">
                    <h3 class="entry-title">
                        <?php echo sanitize_title(the_title( '<a href="' . esc_url(get_permalink($post->ID) ) . '">', '</a>' )); ?></h3>
                </header><!-- .entry-header -->

                <div class="entry-summary">
                    <?php /*echo substr(strip_tags(get_the_excerpt()), 0,999); */?>
                    <?php echo substr(strip_tags(get_the_excerpt()), 0, 120); ?>
                </div><!-- .entry-summary -->
            </div>
            <!-- #thumbnail-->
            <?php
            $cloudinaryImage = get_post_meta($post->ID, 'cloudinary_image_id');
            if ( has_post_thumbnail() ) {
                $thumbnailUrl = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
            }
            ?>

            <?php
            $thumbnail = '';
            if(!empty($cloudinaryImage[0])) {
                $thumbnail = "https://res.cloudinary.com/upload/".$cloudinaryImage[0];
            } else if(!empty($thumbnailUrl)) {
                $thumbnail = $thumbnailUrl[0];
            } ?>
            <a href="<?php echo esc_url(get_permalink($post->ID))?> ">
                <div class="entry-thumb" style="background-image: url('<?= $thumbnail; ?>'); background-repeat: no-repeat; background-size: cover;" alt="">
                </div>
            </a>
            <!-- .thumbnail-image -->
        </article>
            <?php 
                $counter++;
                if ($counter % 5 == 0){
                  echo '<div>Ads Test Div to Be inserted after every 5th post</div>';
                }

            ?>
        <hr>
        <?php

    }
    $counter = 0;
    /* Restore original Post Data */
    wp_reset_postdata();
}
 echo do_shortcode('[ajax_load_more preloaded="false"  offset="13"  exclude"="'.implode(",", $excludePosts).'" images_loaded="false" button_label="Load more" transition_container="false" progress_bar="false" posts_per_page="10" pause="true" pause_override="true" css_classes="infinite-scroll"]');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM