I want two column related thumbnail like this screenshot我想创建两列缩略图相关帖子,例如我已链接到的屏幕截图:
这是到目前为止的代码:
<!---Related Posts --->
<div class="singlerelated">
<div class="headingbig"><div class="shortcode-unorderedlist fa fa-hand-o-right"> Lees meer over <span class="headingorange"><?php the_category(', ') ?></span></div>
</div>
<div class="relatedentry">
<?php
$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category)
$category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 8,
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
while($my_query->have_posts()) {
$my_query->the_post();
?>
<ul>
<li class="even">
<a href="<?php the_permalink() ?> title="<?php the_title(); ?>"><?php the_post_thumbnail(array(100,100), array('class' => 'relatedthumb')); ?> </a>
<p class="title">
<a href="<?php the_permalink()?>"><?php the_title(); ?></a>
</p>
</li>
</ul>
<?
}
}
$post = $orig_post;
wp_reset_query();
?>
</div>
</div>
<!---Related Posts --->
我希望它显示在2列中,通过创建li
类偶数或奇数生成,所以我可以将偶数类放在左侧,而将其他类放在右侧。请帮助我。
答案 0 :(得分:0)
尝试在循环中使用$i
自动增量值。你可能想要一个带有计数的for
,并且如果你有一个奇数的帖子就可以进行四舍五入。如果你不做计数,可能不会结束</ul>
。这是个想法:
$i = 1;
while($my_query->have_posts()) {
$my_query->the_post();
// If $i equals 1, start the <ul>
if($i == 1)
echo '<ul>'.PHP_EOL;
?> <li class="even">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(100,100), array('class' => 'relatedthumb')); ?></a>
<p class="title">
<a href="<?php the_permalink()?>"><?php the_title(); ?></a>
</p>
</li>
<?php
// end on the second loop
if($i == 2) {
echo '</ul>'.PHP_EOL;
// Reset the count back to 0 so it starts
// back at 1 after incrementing
$i = 0;
}
$i++;
}
编辑要做偶数和奇数,您可以使用模数计算:
for($i = 0; $i < 10; $i++)
echo (($i % 2) == 0)? 'even:'.$i.'<br />' : 'odd: '.$i.'<br />';