WP查询在一个div中输出三个项目

时间:2014-09-02 作者:Francesca

目前,我有一个基本查询,输出12个自定义帖子,每个帖子都在一个div中。

我需要对其进行重组,以输出四个div,每个div中有三个这样的项目。所以它有点像一个嵌套的div。

为了解释,这里是当前的HTML输出(用于12个项目)

<div class="item">the content comes out here</div>
<div class="item">the content comes out here</div>
<div class="item">the content comes out here</div>
<div class="item">the content comes out here</div>
<div class="item">the content comes out here</div>
<div class="item">the content comes out here</div>
我需要的是这样的东西:

<div class="jobsOne">
  <div class="item">the content comes out here</div>
  <div class="item">the content comes out here</div>
  <div class="item">the content comes out here</div>
</div>
<div class="jobsTwo">
  <div class="item">the content comes out here</div>
  <div class="item">the content comes out here</div>
  <div class="item">the content comes out here</div>
</div>
以下是我当前的查询:

<?php 
    query_posts(array( 
                \'post_type\' => \'custom_job\',
                \'showposts\' => 12 
            ) );  
        ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="item job">
                    <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                    <p><?php echo get_the_excerpt(); ?></p>
                </div>
        <?php endwhile;?>

3 个回复
SO网友:Tomás Cot

您可以使用变量$current_post, 像这样的。

    <?php 
        $query = new WP_Query(array( 
                    \'post_type\' => \'custom_job\',
                    \'showposts\' => 12 
                ) );  
            ?>
            <?php while ($query->have_posts()) : $query->the_post(); ?>
               <?php if ($query->current_post % 3 === 0) :?>
                 <?php $numbers = array(\'One\', \'Two\'); //add the rest ?>
                 <div class="jobs<?php echo $numbers[floor($query->current_post / 3)];?>">
               <?php endif;?>
               <div class="item">
                        <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                        <p><?php echo get_the_excerpt(); ?></p>
                    </div>

 <?php if ($query->current_post % 3 === 0) :?>
 </div>
<?php endif;?>
            <?php endwhile; wp_reset_postdata();?>
您必须为CSS类名创建一个数组,我将其命名为numbers. 这个floor 函数获取给定实数的最小整数。

这段代码没有经过测试,所以要小心,不要直接在生产中使用它。

SO网友:TheDeadMedic
<?php

$counts = array(
    \'\',
    \'One\',
    \'Two\',
    \'Three\',
    \'Four\',
);

$query = new WP_Query(
    array( 
        \'post_type\' => \'custom_job\',
        \'posts_per_page\' => 12, 
    )
);

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

    if ( $query->current_post + 1 == $query->post_count || ( $query->current_post + 1 ) % 3 === 0 )
        echo \'</div>\'; // End of the loop, or next post will open a new div.

    if ( $query->current_post % 3 === 0 ) // Current post divisible by 3, start new div.
        echo \'<div class="jobs\' . next( $counts ) . \'">\';

    ?>

    <div class="item job">
        <h2><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
        <p><?php echo get_the_excerpt() ?></p>
    </div>

<?php endwhile ?>
SO网友:birgire

这里有另一种方法array_chunk() 如果你不想惹麻烦modulus:

global $post;
$words  = array( \'One\', \'Two\', \'Three\', \'Four\' );
$args   = array( \'posts_per_page\' => 12, \'post_type\' => \'custom_ob\' );
$chunks = array_chunk( get_posts( $args ), $cols = 3 ); // Modify this to your needs. 
foreach( $chunks as $key => $chunk ) {
    printf( \'<div class="job%s">\', $words[$key] ); 
    foreach( $chunk as $post ) {
        setup_postdata( $post ); ?>
         <div class="item job">
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php the_excerpt(); ?></p>
            </div>
    <?php }
    echo \'</div>\'; 
}

结束

相关推荐

使用jQuery和AJAX访问API

作为概念证明,我正在尝试使用jQuery和AJAX访问API,并将结果发布到页面。在完成了一些在线教程并尽最大努力将其整合在一起之后,我有以下几点:功能。php://Fire Customizr require_once( get_template_directory() . \'/inc/init.php\' ); function my_init() { if (!is_admin()) { wp_deregister_script(\'jque