我如何在一个与其他循环完全不同的循环中显示某个帖子(即仅第五个或第六个帖子)


How do I display a certain post (ie. the fifth or sixth post only) in a loop completely different than the rest of the loop?

我想把一个twitter提要显示为博客循环中的一个帖子,所以我想说一些类似的话:"如果第五个帖子,那么使用这个代码(在twitter提要中拉代码)而不是‘content’代码"。我该怎么做?

<?php
        if ( have_posts() ) :
            // Start the Loop.
            while ( have_posts() ) : the_post();
                get_template_part( 'content', get_post_format() );
            endwhile;
            // Previous/next post navigation.
            twentyfourteen_paging_nav();
        else :
            // If no content, include the "No posts found" template.
            get_template_part( 'content', 'none' );
        endif;
    ?>

使用变量计数器,如下所示:

<?php
    if ( have_posts() ) :
        $i = 0;
        $featuredPostNumber = 5; //set this number to whatever you want to use as your featured number
        // Start the Loop.
        while ( have_posts() ) : the_post();
            $i++;
            if($i==$featuredPostNumber){
                echo '<div class="featuredPost">';
                get_template_part( 'content', get_post_format() );
                echo '</div>';
            }else{
                get_template_part( 'content', get_post_format() );
            }
        endwhile;
        // Previous/next post navigation.
        twentyfourteen_paging_nav();
    else :
        // If no content, include the "No posts found" template.
        get_template_part( 'content', 'none' );
    endif;
?>

您的"featurepost"将被包装在一个带有.featuredPost类的div中,然后可以用于不同的样式。