【问题标题】:post loop is repeating same post帖子循环重复相同的帖子
【发布时间】:2017-05-26 10:15:16
【问题描述】:

我的页面设置为连续显示 3 个帖子。然后在 4 列帖子(12 个帖子)之后显示 ajax 加载更多按钮。 问题是在我的代码中我最初只有一个 col-md-4 但它只是在排。所以我添加了两个额外的col-md-4。它现在连续显示 3 个帖子,但每行重复相同的 3 个帖子。有人对此有解决方案吗?

<?php
get_header();
get_template_part ('inc/carousel-food');

$the_query = new WP_Query( array(
    'posts_per_page' => 12,
    'paged' => get_query_var('paged', 1),
    'cat' => 10,
 ));

if ( $the_query->have_posts() ) {

    // display #ajax wrapper only if we have posts
    echo '<div id="ajax">';

    while($the_query->have_posts()) {
          $the_query->the_post(); ?>

        <article <?php post_class(); ?>>    
            <div class="row">
                <div class="col-md-4"><?php the_post_thumbnail('medium-thumbnail'); ?>
                    <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                    <?php get_template_part( 'share-buttons' ); ?>
                    <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                    <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                </div>
                 <div class="col-md-4"><?php the_post_thumbnail('medium-thumbnail'); ?>
                    <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                    <?php get_template_part( 'share-buttons' ); ?>
                    <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                    <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                </div>
                 <div class="col-md-4"><?php the_post_thumbnail('medium-thumbnail'); ?>
                    <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                    <?php get_template_part( 'share-buttons' ); ?>
                    <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                    <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                </div>
            </div>
        </article>

    <?php }//end while

    echo '</div>'; // close the #ajax wrapper after the post list

    if(get_query_var('paged') < $the_query->max_num_pages) {
        load_more_button();
    }


} else { // if there are no posts

    echo '<p>Sorry, no posts matched your criteria.</p>';

}//end if

get_footer();
?>

【问题讨论】:

    标签: php ajax wordpress loops layout


    【解决方案1】:

    我想你想做这样的事情:

    echo '<div class="row">';
    $i = 0;
    while($the_query->have_posts()) {
        $the_query->the_post();
        if($i % 3 == 0){ 
            echo '</div><div class="row">'
        } 
        <div class="col-md-4">
        ...
        </div>
         $i++;
    }
    echo '</div>';
    

    【讨论】:

    • 那么如果我把这段代码放到我上面的代码中会是什么样子呢?每次我尝试将所有内容放在一起时,我都会得到一个空白页。
    • 它的伪代码。如果你添加一个;在这一行的末尾 "echo '
      '" 并用 php echo 包裹 "
      " 和 "
      "声明应该没问题。 “每次我尝试将所有内容放在一起时,我都会得到一个空白页” - 您应该调整错误报告设置,以便 php 打印错误等。
    • 谢谢!我仍然不是最擅长 php。您提供的代码有效,但是帖子连续显示一个,而不是三个。我怎样才能让它连续发三个帖子?
    • if($i % 3 == 0){ echo '
      ' } ,应该做这个工作,我知道为什么不适合你
      跨度>
  • 哦,我知道为什么...我的代码有误:pastebin.com/UGVjvcXD
  • 【解决方案2】:

    在下一个div显示之前使用wp_query的next_post()成员函数,会前进到下一个帖子。

    <?php
    get_header();
    get_template_part ('inc/carousel-food');
    
    $the_query = new WP_Query( array(
        'posts_per_page' => 12,
        'paged' => get_query_var('paged', 1),
        'cat' => 10,
     ));
    
    if ( $the_query->have_posts() ) {
    
        // display #ajax wrapper only if we have posts
        echo '<div id="ajax">';
    
        while($the_query->have_posts()) {
              $the_query->the_post(); ?>
    
            <article <?php post_class(); ?>>    
                <div class="row">
                    <div class="col-md-4"><?php the_post_thumbnail('medium-thumbnail'); ?>
                        <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                        <?php get_template_part( 'share-buttons' ); ?>
                        <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                        <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                    </div>
                    <?php $the_query->next_post(); // advances to next post ?>
                     <div class="col-md-4"><?php the_post_thumbnail('medium-thumbnail'); ?>
                        <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                        <?php get_template_part( 'share-buttons' ); ?>
                        <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                        <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                    </div>
                    <?php $the_query->next_post(); // advances to next post ?>
                     <div class="col-md-4"><?php the_post_thumbnail('medium-thumbnail'); ?>
                        <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                        <?php get_template_part( 'share-buttons' ); ?>
                        <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                        <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                    </div>
                </div>
            </article>
    
        <?php }//end while
    
        echo '</div>'; // close the #ajax wrapper after the post list
    
        if(get_query_var('paged') < $the_query->max_num_pages) {
            load_more_button();
        }
    
    
    } else { // if there are no posts
    
        echo '<p>Sorry, no posts matched your criteria.</p>';
    
    }//end if
    
    get_footer();
    ?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2018-06-19
      • 2015-11-04
      相关资源
      最近更新 更多