WP_QUERY创建html结构

时间:2014-06-21 作者:user51590

我这里有一页来说明我的问题

http://www.ttmt.org.uk.

第一个网格是使用html结构硬编码的,我希望div包含每一行,然后div在其中包含每一块。

我的问题是如何在WP\\U查询中创建此结构。

如果我在WP\\u查询中输出该行,则每行将给我1个块。我要每排三个街区。

我需要循环中的某种循环吗?

<?php get_header(); ?>

    <div class="container">

        <div class="row">
            <div class="col-xs-3 box">
                <p>Post 1</p>
            </div>
            <div class="col-xs-3 box">
                <p>Post 2</p>
            </div>
            <div class="col-xs-3 box">
                <p>Post 3</p>
            </div>
        </div>

        <div class="row">
            <div class="col-xs-3 box">
                <p>Post 4</p>
            </div>
            <div class="col-xs-3 box">
                <p>Post 5</p>
            </div>
            <div class="col-xs-3 box">
                <p>Post 6</p>
            </div>
        </div>

        <div class="row">
            <div class="col-xs-3 box">
                <p>Post 7</p>
            </div>
            <div class="col-xs-3 box">
                <p>Post 8</p>
            </div>
            <div class="col-xs-3 box">
                <p>Post 9</p>
            </div>
        </div>

    </div>


    <div class="container two">

        <h3>With WP_Query</h3>

        <?php
            $grid_args  = array(
                \'post_type\' => \'post\',
                \'order\' => \'ASC\',
                \'orderby\' => \'menu_order\'
            );

            $grid_loop = new WP_Query($grid_args);

            if($grid_loop->have_posts()):
                while($grid_loop->have_posts()):
                    $grid_loop->the_post();

        ?>

        <div class="row">
            <div class="col-xs-3 box">
                <p><?php the_title(); ?></p>
            </div>
        </div>

        <?php
            endwhile;
            endif;
        ?>

        <?php wp_reset_postdata(); ?>
    </div>


<?php get_footer(); ?>

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

使用内置current_postpost_count 带php的变量modulus operator 检查循环中的位置并在适当的时间输出标记。

if( $grid_loop->have_posts() ):

    echo \'<div class="row">\';

    while( $grid_loop->have_posts() ):
        $grid_loop->the_post();

        ?>
        <div class="col-xs-3 box">
            <p><?php the_title(); ?></p>
        </div>
        <?php

        // if this is the third post
        // and not the last post
        // close previous row and open a new one.
        // note: current_post starts at zero, so we add 1 to it
        if( ((($grid_loop->current_post + 1) % 3) == 0)
            && ($grid_loop->current_post + 1) != $grid_loop->post_count  ):
                echo \'</div><div class="row">\';
        endif;

    endwhile;

    echo \'</div>\';

endif;

结束

相关推荐

使用wp_mail发送的HTML电子邮件显示纯文本

我有一张要发送的HTML发票。我使用以下函数替换HTML中的某些内容 foreach($variables as $key => $value){ $template = str_replace(\'{{ \'.$key.\' }}\', $value, $template); } 之后,我发送邮件:$to = get_option(\'admin_email\'); $subject = \"Someone reserved from \".get_blogi