不同的循环显示同一类别中的不同帖子

时间:2014-09-08 09:58:03

标签: php wordpress

我在发布帖子时遇到了一些问题。

我要做的是我想要显示一个类别的帖子,但条件是第一个帖子我想在循环中显示在不同的div。

然后在同一类别的第二和第三篇文章中,我希望在另一个div和不同的循环中以不同的循环和相同类别的帖子的其余部分显示。 即

<div class="post1"> 1st post in the selected category </div>
<div class="post23"> 2nd and 3rd posts in selected  category </div>
<div class="post--"> rest all posts in selected category </div>

这是我的代码

 <?php $i = 0;
                $args = array( 'offset'=> 0, 'category' => 19, 'numberposts' =>2000, 'order' => 'DESC');
                $myposts = get_posts( $args );
                foreach ( $myposts as $post ) : setup_postdata( $post );
             if ($i ==1 ) { 

               ?>
            <li>
            <div class="cover_post">
             <div class="image_bl"><?php the_post_thumbnail(array(320,245)); ?></div>
             <div class="content_hover">
              <h6>By : <?php the_author(); ?></h6>
             <h3 class="title"><?php echo get_the_title(); ?></h3>
            <p> <?php //the_excerpt(); ?> <?php echo substr(get_the_excerpt(), 0,70); ?> ...</p>
             <p><a href="<?php the_permalink(); ?>">Read More</a> 
             <a href="#">SHARE <span class="facebook_share"> <img src="<?php echo get_template_directory_uri(); ?>/images/facebook_sh.png" alt=""  /></span> <span class="twitter_share"> <img src="<?php echo get_template_directory_uri(); ?>/images/twit_sh.png" alt=""  /></span></a>
             </p>
             </div>
             <div class="content_hover content_hover1" style=" visibility:visible">
              <h6>By : <?php the_author(); ?></h6>
             <h3 class="title"><?php echo get_the_title(); ?></h3>

             </div>
             <div class="clear"></div>

             </div>
             <div class="icon_post">

</div>
                 </li>
            <?php  $i++; }          
            endforeach;
                wp_reset_postdata();
            ?>

有人能帮助我吗?

先谢谢

3 个答案:

答案 0 :(得分:0)

只需在循环开始时添加条件即可。类似的东西:

switch ($i) {
    case 0:
        echo '<div class="post1">';
        break;
    case 1:
    case 2:
        echo '<div class="post23">';
        break;
    default:
        echo '<div class="post">';
        break;
}

然后在循环结束时关闭</div>

答案 1 :(得分:0)

<?php
if($i == 0)
{
?>
    <div> Div for first post</div>
<?php
}elseif($i == 1 || $i == 2)
{

?>
    <div> Div for second and third posts</div>
<?php
}
else
{
?>
    <div> Div for rest posts</div>
<?php
}
?>

答案 2 :(得分:0)

解:

我为此

使用了不同的偏移
 <?php 
     $args = array( '**offset**'=> 0, 'category' => 19, 'numberposts' =>1, 'order' => 'DESC');
     $myposts = get_posts( $args );
     foreach ( $myposts as $post ) : setup_postdata( $post );

 ?> 

  <?php 
         $args = array( '**offset**'=> 1, 'category' => 19, 'numberposts' =>2, 'order' => 'DESC');
         $myposts = get_posts( $args );
         foreach ( $myposts as $post ) : setup_postdata( $post );
        { 
     ?>


<?php 
             $args = array( '**offset**'=> 3, 'category' => 19, 'numberposts' =>5, 'order' => 'DESC');
             $myposts = get_posts( $args );
             foreach ( $myposts as $post ) : setup_postdata( $post );
            { 
         ?>