douwei7976 2014-03-04 07:33
浏览 93
已采纳

foreach语句中的wordpress循环

Im trying to make a program for a radio show website. I have a custom field with all the days of week and I just wanna put each post in list of its corresponding day.

So far Iv got the code below and I make 7 lists for all the days but each list has all the same posts. Anyone know how to make this work? Or something else would be better?

<?php
$days = array(
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
    'Sunday',
);

$guide = array(
    'post_type' => 'shows',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'show_day',
            'value' => $dayg,
            'compare' => 'like'
            )
        )
    );
?>

<h3 class="guide">
    <?php the_title(); ?>
</h3>
<?php foreach($days as $dayg) { ?>

    <ul class="day">
        <h3>
            <?php echo $dayg; ?>
        </h3>

        <?php query_posts($guide); while(have_posts()) : the_post(); ?>

        <li><?php the_title(); echo ' | ' , the_field('show_day'); ?></li>

        <?php endwhile; wp_reset_query(); ?>

    </ul>

<?php } ?>

Anyone know

  • 写回答

1条回答 默认 最新

  • doubi4435 2014-03-05 05:19
    关注

    Replace your code with this:--

    <?php
    $days = array(
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
    );
    ?>
    
    <h3 class="guide">
        <?php the_title(); ?>
    </h3>
    <?php foreach($days as $dayg) { ?>
    
    <?php 
    $guide = array(
            'post_type' => 'shows',
            'posts_per_page' => -1,
            'meta_query' => array(
                    array(
                            'key' => 'show_day',
                            'value' => $dayg,
                            'compare' => 'like'
                    )
            )
    );
    ?>
        <ul class="day">
            <h3>
                <?php echo $dayg; ?>
            </h3>
            <?php query_posts($guide); while(have_posts()) : the_post(); ?>
    
            <li><?php the_title(); echo ' | ' , the_field('show_day'); ?></li>
    
            <?php endwhile; wp_reset_query(); ?>
        </ul>
    <?php } ?>
    

    This will work, because you are adding $guide variable outside the loop thats why $dayg is not set...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播