image - WordPress获取图像URL(当前使用wp_get_attachment_url)

标签 image wordpress url attachment

在wordpress中获取图片附件网址时遇到了一些麻烦。到目前为止,这是我的代码:

<?php // find images attached to this post / page.
            global $post;
            $thumb_ID = get_post_thumbnail_id( $post->ID );
            $args = array(
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'numberposts' => -1,
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'exclude' => $thumb_ID,
                'post_parent' => $post->ID
            );
            $images = get_posts($args);
            ?>

            <?php // Loop through the images and show them

            if($images)
            {
            foreach($images as $image)  
            {

            echo  wp_get_attachment_image_url($image->ID, $size='attached-image');

            }
            }

?>

这什么也没返回。如果我将wp_get_attachment_image_url($image->ID, $size='attached-image');换成wp_get_attachment_image($image->ID, $size='attached-image');,则效果很好,但会引入图像,而不只是URL。

最佳答案

以下代码将遍历所有图像附件并输出src url。请注意,根据您的需要,显示了两种方法。

<?php

    global $post;
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID ); 
    $attachments = get_posts($args);
    if ($attachments) {
            foreach ( $attachments as $attachment ) {
                    // Method #1: Allows you to define the image size
                    $src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
                    if ($src) {echo $src[0];}
                    // Method #2: Would always return the "attached-image" size
                    echo $attachment->guid;
            }
    }

?>

关于image - WordPress获取图像URL(当前使用wp_get_attachment_url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389519/

相关文章:

css - 修复 google minify css url screwup

Powershell:下载文件 404 但站点存在

android - 在android中下载图像的想法

html&css 将白色箭头设置为底部图像

java - 绘制图像后设置的像素发生变化

php - 用于自定义产品图像缩略图的 Hook

java - 查找图像的最小可视矩形的尺寸

mysql - Wordpress 到 Jekyll 脚本抛出错误

php - WordPress 在 if 条件下触发 Action 过滤器

php - 如何强制 cURL 发送完整的 URL 作为请求目标?