在WordPress中创建两个与列相关的缩略图


Create Two Column Related Thumbnails in Wordpress

我想要两列相关的缩略图,如此屏幕截图我想创建两列缩略图相关的帖子,如我链接到的屏幕截图所示:

这是到目前为止的代码:

<!---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类交替生成偶数或奇数,这样我就可以将偶数类放在左侧,其他放在右侧。请帮助我。

尝试在循环中使用$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 />';