Wordpress 查询相关帖子,每3篇帖子换行一次
我试图查询一些相关的文章,并将它们每3篇文章包装成一个行div。我已经检查了所有关于这个的问题,但我的问题仍然是一团糟。 这就是我到目前为止所做的:Wordpress 查询相关帖子,每3篇帖子换行一次,wordpress,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=
<?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(); ?>
提前感谢使用下面的代码
<?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();
?>
使用下面的代码
<?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();
?>
非常感谢@shital marakana,但它仍然将行输出到行中。更改为?正确的。请检查更新的答案。它成功了!还有一件事,我如何防止在末尾创建一个空行?现在用更新的答案检查。如果有任何问题,请告诉我@shital marakana,但它仍然将行输出到行中。请更改为?正确的。请检查更新的答案。它成功了!还有一件事,我如何防止在末尾创建一个空行?现在用更新的答案检查。如果有任何问题,请告诉我