有没有一种方法可以按顺序乘以归档循环结果?

时间:2015-09-17 作者:RLM

我正在尝试制作自己的版本of this 在我的尝试中,我已经实现了引导网格。我很接近,但是<div class="row"> 循环导致了问题。

我只想用引导程序构建我的html结构,而不是完全依赖if 控制吐出多少存档的语句。

有没有办法添加尽可能多的<?php the_post_thumbnail(\'category-thumbnail\'); ?> (例如)如我所愿,它会按顺序将博客吐出吗?

<?php
$count = 0;
while ( have_posts() ) : the_post();

if ( $count < 1 ) { ?>
<div class="row" style="margin-bottom:0px;">
    <div class="col-md-12">
        <a class="thumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'category-thumbnail\'); ?></a>
        <div class="blog-details-wrapper clear">
            <h2 class="title">
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </h2>
            <span class="date"><?php the_time(\'F j, Y\'); ?> <?php the_time(\'g:i a\'); ?></span>
            <span class="author"><?php _e( \'Published by\', \'html5blank\' ); ?> <?php the_author_posts_link(); ?></span>
            <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( \'Leave your thoughts\', \'html5blank\' ), __( \'1 Comment\', \'html5blank\' ), __( \'% Comments\', \'html5blank\' )); ?></span>
        </div>
    </div>
</div>

<?php } elseif ( $count <= 2 ) { ?>
  <div class="row" style="margin-bottom:0px;">
    <div class="col-md-6">
        <a class="thumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'category-thumbnail\'); ?></a>
        <div class="blog-details-wrapper clear">
            <h2 class="title">
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </h2>
            <span class="date"><?php the_time(\'F j, Y\'); ?> <?php the_time(\'g:i a\'); ?></span>
            <span class="author"><?php _e( \'Published by\', \'html5blank\' ); ?> <?php the_author_posts_link(); ?></span>
            <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( \'Leave your thoughts\', \'html5blank\' ), __( \'1 Comment\', \'html5blank\' ), __( \'% Comments\', \'html5blank\' )); ?></span>
        </div>
    </div>
    <div class="col-md-6">
        <a class="thumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'category-thumbnail\'); ?></a>
        <div class="blog-details-wrapper clear">
            <h2 class="title">
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </h2>
            <span class="date"><?php the_time(\'F j, Y\'); ?> <?php the_time(\'g:i a\'); ?></span>
            <span class="author"><?php _e( \'Published by\', \'html5blank\' ); ?> <?php the_author_posts_link(); ?></span>
            <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( \'Leave your thoughts\', \'html5blank\' ), __( \'1 Comment\', \'html5blank\' ), __( \'% Comments\', \'html5blank\' )); ?></span>
        </div>
    </div>
</div>


<?php } $count++;
endwhile;

?>
所以在上面的代码中:我可以在每一行中调用一个或多个博客,但它将按顺序调用下一个博客。因此,在这种情况下:一个覆盖整个浏览器,然后两个50%宽度的浏览器紧挨着另一个,然后为更多的博客重复这种结构。

这可能吗?有没有一种方法可以生成一个变量或其他东西,允许您重复调用归档文件,同时按顺序吐出归档文件,而不是重复相同的归档文件?

提前谢谢。

2 个回复
SO网友:RLM
<?php
$counter = 1; 

if(have_posts()) :  while(have_posts()) :  the_post(); 
?>
<?php
if($counter == 1) :
?>
            <div class="row clear" style="margin-bottom:0px;">
                <div class="col-md-12 border-12">
                    <a class="thumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'category-thumbnail\'); ?></a>
                    <div class="blog-details-wrapper clear">
                    <h2 class="title">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                    </h2>
                        <span class="date"><?php the_time(\'F j, Y\'); ?> <?php the_time(\'g:i a\'); ?></span>
                        <span class="author"><?php _e( \'Published by\', \'html5blank\' ); ?> <?php the_author_posts_link(); ?></span>
                        <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( \'Leave your thoughts\', \'html5blank\' ), __( \'1 Comment\', \'html5blank\' ), __( \'% Comments\', \'html5blank\' )); ?></span>
                    </div>
                </div>
            </div>

<?php

elseif($counter == 2) :
?>

            <div class="row">
                <div class="col-md-6 border">
                    <a class="thumb-6" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'category-thumbnail\'); ?></a>
                    <div class="blog-details-wrapper clear">
                    <h2 class="">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                    </h2>
                        <span class="date"><?php the_time(\'F j, Y\'); ?> <?php the_time(\'g:i a\'); ?></span>
                        <span class="author"><?php _e( \'Published by\', \'html5blank\' ); ?> <?php the_author_posts_link(); ?></span>
                        <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( \'Leave your thoughts\', \'html5blank\' ), __( \'1 Comment\', \'html5blank\' ), __( \'% Comments\', \'html5blank\' )); ?></span>
                    </div>
                </div>

<?php
elseif($counter >= 2) : ?>

                <div class="col-md-6 border">
                    <a class="thumb-6" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'category-thumbnail\'); ?></a>
                    <div class="blog-details-wrapper clear">
                    <h2 class="">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                    </h2>
                        <span class="date"><?php the_time(\'F j, Y\'); ?> <?php the_time(\'g:i a\'); ?></span>
                        <span class="author"><?php _e( \'Published by\', \'html5blank\' ); ?> <?php the_author_posts_link(); ?></span>
                        <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( \'Leave your thoughts\', \'html5blank\' ), __( \'1 Comment\', \'html5blank\' ), __( \'% Comments\', \'html5blank\' )); ?></span>
                    </div>
                </div>
            </div>  

<?php 
$counter = 0;
endif;
?>
<?php
$counter++;
endwhile;
endif;
?>
SO网友:Rarst

常见的循环实现为每个post循环。它是如此普遍,以至于不明显它是完全可选的。

“每个”部分来自while ( have_posts() ), 前进自the_post(). 前者在技术上是完全可选的。

你只需打个电话就可以提前一个帖子the_post() 并用你认为必要的任何其他逻辑来围绕它。当然,如果您的需求更复杂,那么也需要比简单更复杂的逻辑while().

我以前在博客上写过这件事Asymmetrical WordPress loops.

相关推荐

Blog Post slider not working

最近我一直在尝试为我的博客帖子创建自己的滑块,但我无法让它工作。我在网上尝试了不同的代码,但没有一个可以旋转我的列表项(这些是通过post循环创建的)。下面是我尝试的代码W3Schools 但它显示了一个错误Uncaught TypeError: Cannot read property \'style\' of undefined at carousel这是我的函数。我在索引上调用的php。php显示帖子:function displayBlogSlider() { $args = a