在 WordPress 中组合 foreach 语句

发布于 2025-01-02 03:47:42 字数 676 浏览 0 评论 0原文

我有一个 Wordpress 网站,并且有一个画廊,其中列出了每个项目的图像和 url。但是我似乎无法合并 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; ?>

I have a Wordpress website and I have a gallery listed with an image and url for each item. However I can't seem to combine the foreach statements. I always get errors. I know I am close, I think I just need to re-arrange things a bit. Thank you!

<?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; ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2025-01-09 03:47:42

你只有一个 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

you only have one closing foreach statement. Other than that the code looks good. I would do a little better indenting so that you can see.

<?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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文