同一类别上具有不同设计的多个循环

时间:2019-08-06 作者:aakash

我有一个问题,关于显示类别帖子上的多个循环,我想从category=social 3设计位置的结构如下enter image description here

Note: 我想要一个没有重复的帖子

这是我面临问题的代码

           <?php 
           $args=array(
              \'posts_per_page\' => 1,
              \'category_name\' => \'social\'
                    );
           $my_query = new WP_Query($args);
             while ($my_query->have_posts()) : $my_query->the_post(); 

             if( 1 > $wp_query->current_post ): ?>
        <article class="post post--overlay post--overlay-xs post--overlay-floorfade post--overlay-bottom cat-1">
           <?php 
           $image = get_the_post_thumbnail_url(get_the_ID(),\'full\');

            ?>
           <div class="background-img" style="background-image: url(<?php echo $image; ?>)"></div>
           <div class="post__text inverse-text">
              <div class="post__text-wrap">
                 <div class="post__text-inner">
                    <h3 class="post__title typescale-2"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

                 </div>
              </div>
           </div>
        </article>
        <?php else: break; endif; endwhile; wp_reset_postdata(); ?>

        <div class="spacer-xs"></div>
        <ul class="list-space-xs list-seperated list-square-bullet-exclude-first list-unstyled">
           <?php 
           $wp_query->current_post = 2;
           while ( have_posts() ) : the_post();
            ?>
           <li>
              <article class="post--horizontal post--horizontal-xs cat-2">
                 <div class="post__thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array( 100, 75 ) ); ?></a></div>
                 <div class="post__text">
                    <h3 class="post__title typescale-1"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

                 </div>
              </article>
           </li>
           <?php endwhile; ?>

           <?php 
           // Problem in showing the post in the third loop 

           while(have_posts()): the_post(); ?>
           <li>
              <article class="cat-4">
                 <div class="post-content">
                    <h3 class="post__title typescale-0"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                 </div>
              </article>
           </li>
        <?php endwhile; ?>

        <?php  ?>

        </ul>
我从以下网站获得了帮助Multiple Loops Homepage?

1 个回复
最合适的回答,由SO网友:nmr 整理而成

在第一个循环中,显示来自social 类别,因此应将其放在代码末尾。您只查询该类别中的一篇文章:\'posts_per_page\' => 1.

<?php 
//
// display first post
while( have_posts() )
{
    the_post();

    ?>
    <article class="">

    </article>
    <?php 
    // exit from loop after first posts (count from 0)
    if ( $wp_query->current_post >= 0 )
        break;
}

?>
<div class="spacer-xs"></div>
    <ul class="list-space-xs list-seperated list-square-bullet-exclude-first list-unstyled">
    <?php

    //
    // display second post
    while( have_posts() )
    { 
        the_post();
        ?>
        <li>
            <article class="post--horizontal post--horizontal-xs cat-2">

            </article>
        </li>
        <?php 

        // exit from loop after second posts
        if ( $wp_query->current_post >= 1 )
            break;
    }

    //
    // display posts from "category=social"
    $args=array(
        \'posts_per_page\' => 3, // <-- 
        \'category_name\' => \'social\'
    );
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) : $my_query->the_post();
    ?>
        <li>
            <article class="cat-4">
            </article>
        </li>
    <?php endwhile; ?>


    ?>
    </ul>
</div>
<?php

相关推荐

Get category url in loop

我需要获取类别url,以便将其放入元代码段中。现在,span内的输出是带有url的标记。我只需要获取不带标记的url,并将其放入$cat\\u display中。我尝试使用2个选项,但会看到一个错误:注意:尝试获取非对象的属性// Get post category info $category = get_the_category(); if(!empty($category)) { // Get last category post is in