1

我正在构建一个基于 Twitter Bootstrap 的 Wordpress 主题。Bootstrap 的响应式布局.row-fluid跨越了 100% 的页面,并且可以在一行中有 12 个“跨度”。

<div class="row-fluid">
    <?php query_posts('category_name=feature-articles&showposts=6'); ?> 
    <?php while (have_posts()) : the_post(); ?>

        <div class="span4">
        <div class="main-thumb"><?php echo get_the_post_thumbnail(($page->ID) , 'main-thumb'); ?></div>
        <a href="<?php the_permalink(); ?>"><h3 class="title"><?php the_title(); ?></h3></a>
        <p class="excerpt"><?php
            $my_excerpt = get_the_excerpt();
            if ( $my_excerpt != '' ) {
                // Some string manipulation performed
            }
            echo $my_excerpt; // Outputs the processed value to the page
            ?></p>


    <?php endwhile; ?></div>

这将创建 6 个“span4”,我想跨越 2 个不同的行 - 但由于循环,.row-fluid在创建 3 个帖子后无法关闭原始 div 并打开另一个。

为简单起见,我想获取帖子 1、2 和 3,然后关闭.row-fluiddiv 并创建另一个,然后获取帖子 4、5 和 6。这可以通过一些循环操作来实现吗?

4

1 回答 1

4

您必须在循环中放置一个计数器,当它达到 3 时,添加一个关闭/打开标签。我还添加if(have_posts())到循环的开头,以避免错误(如果没有帖子,也允许您输出消息)。

<div class="row-fluid">

    <?php $i = 0 ?>
    <?php query_posts('category_name=feature-articles&showposts=6'); ?>
    <?php if(have_posts()) : while (have_posts()) : the_post(); ?>

        <?php if( $i == 3 ) : ?>
            </div>
            <div class="row-fluid">
        <?php endif; ?>

        <div class="span4">

        <div class="main-thumb">
            <?php echo get_the_post_thumbnail(($page->ID) , 'main-thumb'); ?>
        </div>
        <a href="<?php the_permalink(); ?>">
            <h3 class="title"><?php the_title(); ?></h3>
        </a>
        <p class="excerpt">
<?php
            $my_excerpt = get_the_excerpt();
            if ( $my_excerpt != '' ) {
                // Some string manipulation performed
            }
            echo $my_excerpt; // Outputs the processed value to the page
?>
        </p>

        <?php $i++ ?>

        <?php endwhile; ?>
    <?php endif; ?>

</div> 
于 2012-10-24T11:25:37.350 回答