php - 在 Wordpress 中创建两列相关的缩略图

标签 php css wordpress

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

最佳答案

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

关于php - 在 Wordpress 中创建两列相关的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35960347/

相关文章:

php - 评估给定文本 block 的关键字密度

css - 在我的文本区域中将文本从顶部向下推几个像素?

javascript 在本地服务器上工作,但在实时服务器上不起作用

php - 更新数据库表行

php - 处理 codeigniter 和 phpactiverecord 时出错

javascript - Google reCAPTCHA 第二次提交失败

PHP mysql 将表行与 json 数组进行比较

javascript - 简单的 js 添加 3d

css - 如何让自定义 CSS 按钮根据图像自动调整大小

wordpress - Nginx,Wordpress和SQL