我在页面的某个部分中添加了帖子,在其中我编写了一个查询以仅显示两个最新的帖子,它们的一侧在缩略图上,并且文本与缩略图平行,我想做的是每次迭代后都希望显示反转,即:在第一次迭代中,缩略图将在右侧,文本在左侧,而在下一次迭代中,我希望缩略图在左侧,文本在右侧。

这是我的帖子代码:

<div class="container">
    <div class="col-md-12">
        <h2 class="main-hadding">our blog news</h2>
    </div>
    <div class="blog-section">
        <div class="row">

    <?php
//display 2 posts for category id 47
    $args=array(
    //  'cat' => 47,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 2,
      'caller_get_posts'=> 1
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {

      while ($my_query->have_posts()) : $my_query->the_post();
          $Post_ID = get_the_ID ();


      ?>

<!--        Post Thumbnail-->
          <div class="col-md-6 col-sm-6">
              <?php
if ( has_post_thumbnail() ) {
    the_post_thumbnail();
}
     ?>
        </div>

<!--        Post Title and Content-->
     <div class="col-md-6 col-sm-6">
            <div class=" blog-contant">
                <h1> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                <p><?php the_excerpt();?></p>
            </div>
        </div>

       <?php
      endwhile;
    }
wp_reset_query();  // Restore global post data stomped by the_post().
?>
      </div>


 </div>
    <div class="col-md-12 text-center btn-more"> <a href="#!" class="">MORE BLOG NEWS</a> </div>

</div>

最佳答案

一个漫长而肮脏的解决方案,但这将起作用..:D

$count = 0;
while ($my_query->have_posts()) :
    $my_query->the_post();
    $Post_ID = get_the_ID ();
    if($count == 0) : ?>
        <!--Post Thumbnail-->
        <div class="col-md-6 col-sm-6">
            <?php
                if ( has_post_thumbnail() ) {
                    the_post_thumbnail();
                }
            ?>
        </div>

        <!--Post Title and Content-->
        <div class="col-md-6 col-sm-6">
            <div class=" blog-contant">
                <h1> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                <p><?php the_excerpt();?></p>
            </div>
        </div>
    <?php else: ?>

        <!--Post Title and Content-->
        <div class="col-md-6 col-sm-6">
            <div class=" blog-contant">
                <h1> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                <p><?php the_excerpt();?></p>
            </div>
        </div>
        <!--Post Thumbnail-->
        <div class="col-md-6 col-sm-6">
            <?php
                if ( has_post_thumbnail() ) {
                    the_post_thumbnail();
                }
            ?>
        </div>
    <?php
   endif;
   $count++;
endwhile;

关于php - 每次迭代后,有什么方法可以反转帖子的布局吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36419929/

10-09 16:22