php - 获取 Wordpress 附件图片 url

标签 php wordpress

我正在尝试显示附加到 wordpress 帖子的所有图像,并将图像链接到图像源以在灯箱中打开它。我正在使用以下代码:

if (have_posts())
{
    while (have_posts())
    {
        the_post();    
        $args = array(
            'orderby' => 'title',
            'order' => 'ASC',
            'post_type' => 'attachment',
            'numberposts' => -1,
            'post_status' => null,
            'post_parent' => $post->ID,
            'exclude' => get_post_thumbnail_id()
        );
        $attachments = get_posts( $args );
        if ($attachments)
        {
            foreach ($attachments as $attachment)
            {
                ?>
                <div id="image">
                    <a rel="lightbox" href="<?php /* ??? */ ?>">
                    <?php echo wp_get_attachment_image ($attachment->ID, 'full'); ?>
                    </a>
                </div>
                <?php
            }
        }
    }
}

我在 ??? 中尝试了很多东西,但我似乎无法只检索源 url。

谁能提供一些见解?

最佳答案

使用wp_get_attachment_image_src

https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

这个:

wp_get_attachment_image_src( $attachment->ID, 'full' );

应该返回一个包含以下元素的数组

[0] => url
[1] => width
[2] => height
[3] => boolean: true if $url is a resized image, false if it is the original or if no image is available.

关于php - 获取 Wordpress 附件图片 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30810542/

相关文章:

php - 逻辑异常错误 : Passed array does not specify an existing static method

php - 如何在亚马逊 MWS 中发布产品?

wordpress - 在 RSS 提要中为 Wordpress 博客启用 MathJax

css - 无法识别 WP 网站上对 CSS 的更改

php - 如何修复站点地图得到错误详细信息 : 1 Errors, 0 警告。?

php - 如何使用 PHP 向多个用户发送消息

php - 按惯例编码是否会妨碍灵活性?

java - 连接到 Android 的 php 文件

php - Woocommerce:结帐订单评论字段中的订单摘要(PHP)

mysql - 经常无法连接到数据库