查询相关帖子,每隔3个帖子换行

时间:2017-07-07 10:56:24

标签: wordpress

我正在尝试查询一些相关的帖子,并将它们包含在每3个帖子的行div中。我已经检查了所有关于此的问题,但我的查询仍然造成混乱。 这就是我到目前为止所做的:

<?php
$related = get_posts( array(
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 6,
'post__not_in' => array($post->ID) ) );
$counter=0;
if( $related ) foreach( $related as $post ) {
setup_postdata($post); $counter++;
 ?>

<div class="row">
    <article class="third-width">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">           
            <?php  the_post_thumbnail('post-parrilla'); ?>
        </a>
<?php exclude_post_categories("8"); ?>
<div class="clear"></div>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">           
        <div class="post-title">
        <span><?php the_title(); ?></span>
        </div>
    </a>
    </article>

<?php if ($counter%3==0):?>
    </div><div class="row"> 
<?php endif;?>
<?php } wp_reset_postdata(); ?>

提前谢谢

1 个答案:

答案 0 :(得分:2)

使用以下代码

 <?php
$related = get_posts( array(
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 6,
'post__not_in' => array($post->ID) ) );

if( $related ) 
{
    $counter = 0;
    ?>
    <div class="row">
    <?php
    foreach( $related as $post ) {
    setup_postdata($post); 

     if ($counter%3==0){
     ?>
        </div><div class="row"> 
       <?php 
     }
    ?>

        <article class="third-width">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">           
                <?php  the_post_thumbnail('post-parrilla'); ?>
            </a>
    <?php exclude_post_categories("8"); ?>
    <div class="clear"></div>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">           
            <div class="post-title">
            <span><?php the_title(); ?></span>
            </div>
        </a>
        </article>

     <?php
     $counter++;
    } 
    ?>
    </div>
    <?php
}
wp_reset_postdata(); 
?>