【问题标题】:Combining foreach statements in Wordpress在 Wordpress 中结合 foreach 语句
【发布时间】:2012-02-05 02:57:27
【问题描述】:

我有一个 Wordpress 网站,并且我有一个画廊,其中列出了每个项目的图片和网址。但是我似乎无法结合 foreach 语句。我总是出错。我知道我很接近,我想我只需要重新安排一下。谢谢!

<?php 
foreach($latest_projects as $post) : setup_postdata($post);
//get image
$recent_project_thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'recent-work');

foreach ($categories as $cat ) :
//get image
$categories = get_terms('gallery_cats');
?> 

<li><a href="https://website.com/<?php echo $cat->slug; ?>"><img src="<?php echo $recent_project_thumb[0]; ?>" alt="<?php the_title(); ?>" width="162" height="100" class="imgstyle" /></a></li>

<?php endforeach; ?>

【问题讨论】:

    标签: wordpress foreach


    【解决方案1】:

    您只有一个结束 foreach 语句。除此之外,代码看起来不错。我会做一些更好的缩进,以便您可以看到。

    <?php 
    foreach($latest_projects as $post) : //begin post foreach
        setup_postdata($post);
    
        //get image
        $recent_project_thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'recent-work');
    
        foreach ($categories as $cat ) : //begin categories foreach
            //get image
            $categories = get_terms('gallery_cats');
    ?> 
    
            <li>
                <a href="https://website.com/<?php echo $cat->slug; ?>">
                <img src="<?php echo $recent_project_thumb[0]; ?>" alt="<?php the_title(); ?>" width="162" height="100" class="imgstyle" />
                </a>
            </li>
    
        <?php endforeach; ?> //end categories foreach
    <?php endforeach; ?> //end post foreach
    

    【讨论】:

    • 感谢您的帮助,但我不想有 2 个循环,我实际上想以某种方式将它们组合起来,这就是为什么我只放了一个 声明在最后。我在 foreach ($categories as $cat ) 也遇到错误感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 2020-07-21
    • 2015-12-28
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多