douwei7976 2014-03-03 23: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-04 21: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...

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部