【问题标题】:Loop 6 posts inside 3 li elements在 3 li 元素内循环 6 个帖子
【发布时间】:2019-12-26 01:12:36
【问题描述】:

如何在 3 个 li 元素内循环 6 个帖子。我想告诉我的代码为每个 li 元素放置 2 篇文章。

<ul class="seminar-list">
    <?php
        $args = array(
        'post_type' => 'topics',
        'posts_per_page' => 6,
        'tax_query' => array(
        array(
            'taxonomy' => 'topicscat',
            )
            )
        );

     $seminarQuery = new WP_Query($args);
     $idx = 1;
     if($seminarQuery->have_posts()): while($seminarQuery->have_posts()): $seminarQuery->the_post();
                        ?>
        <li>
            <a href="topicscat/seminar-info/#event<?php echo $idx ?>">
                <time><?php echo get_field('event-date') ?></time>
            <span class="location"><?php echo get_field('location') ?> 
            </span>
            </a>
        </li>
    <?php $idx++; endwhile; wp_reset_postdata(); endif; ?>
</ul>

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    您可以将&lt;li&gt;标签的生成带入PHP代码中,仅在第1、3、5...帖子开始&lt;li&gt;,仅在第2、第4、第6...帖子结束...

     if($seminarQuery->have_posts()): while($seminarQuery->have_posts()): $seminarQuery->the_post();
        if ($idx % 2 == 1) echo "<li>";
                        ?>
            <a href="topicscat/seminar-info/#event<?php echo $idx ?>">
                <time><?php echo get_field('event-date') ?></time>
            <span class="location"><?php echo get_field('location') ?> 
            </span>
            </a>
    <?php 
        if ($idx % 2 == 0) echo "</li>";
        $idx++; endwhile; wp_reset_postdata(); endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 2019-04-03
      相关资源
      最近更新 更多