【问题标题】:Get single Wordpress gallery image with given title获取具有给定标题的单个 Wordpress 画廊图像
【发布时间】:2011-02-08 02:11:00
【问题描述】:

我让 Wordpress 博客的用户将一张名为“banner”的图片上传到不同帖子的画廊。基本上,每个帖子都会有一张名为“横幅”的图片上传到其图库,并且该图片需要显示在文章内容之外的帖子页面上。那么,如何获取 URL 以在帖子的 single.php 模板中显示图像?

我能否遍历给定帖子图库的图片并以某种方式找到具有正确标题的图片?

我搜索了 Wordpress 的 codex 文档,但没有找到任何方法,只有关于显示照片库的信息。请注意,我已经将 Wordpress 的帖子缩略图功能用于其他用途。

谢谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我想我终于想通了。我想不通的部分是使用模糊命名的get_children() 函数。

    <?php
    have_posts(); //must be in the loop
    the_post(); //set the ID
    $images =& get_children(array('post_mime_type' => 'image', 'post_parent' => get_the_ID()));
    rewind_posts(); //rewind for the actual loop
    
    $image_url = null;
    foreach($images as $image) {
        if($image->post_title == 'banner') {
            $image_url = $image->guid;
            break;
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 2015-05-25
      • 1970-01-01
      相关资源
      最近更新 更多