doushan3511
doushan3511
2018-08-07 12:14

Post循环在WordPress上无法正常工作

已采纳

I created a separate page for blog posts on my website (WordPress). I Used this code for showing 6 last posts:

<div class="container">

    <div class="entry-content">

        <div class="last-posts">

            <?php $the_query = new WP_Query( 'posts_per_page=6' ); ?>
            <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
            <div class="col-md-4">
                <div class="post-item">
                    <a href="<?php the_permalink() ?>" target="_blank">
                        <figure><?php the_post_thumbnail(); ?></figure>
                    </a>
                    <div class="post-detail">
                        <h3>
                            <a href="<?php the_permalink() ?>" target="_blank">
                                <span><?php the_title(); ?></span>
                            </a>
                        </h3>
                    </div>
                </div>
            </div>
            <?php
            endwhile;
            wp_reset_postdata();
            ?>
            
        </div>

    </div>
</div>

The problem is, this loop works from "container" class to the end of the code, not just 6 last posts. This loads all 6 posts 6 times.

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongshan7708 dongshan7708 3年前

    HI you are making a small mistake in the WP_Query please try the following line.

    <?php $the_query = new WP_Query( 'posts_per_page' => 6 ); ?>
    

    I hope this will work for you.

    点赞 评论 复制链接分享
  • douanrang4728 douanrang4728 3年前
    $args = array('posts_per_page' =>6, 'post_type' => 'post','order'=>'desc','post_status'=>'publish');
    $the_query = new WP_Query( $args );   
    
    点赞 评论 复制链接分享