php 从 Wordpress 媒体库中获取单个特定图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20664356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:23:53  来源:igfitidea点击:

Get a single specific image from Wordpress Media Library

phphtmlwordpressimage

提问by Richard

I've uploaded images to Wordpress Media Library.

我已将图片上传到 Wordpress 媒体库。

I understand that I can view am image then get the URL for that specific image and then use the imghtml tag to display this on the page.

我知道我可以查看图像,然后获取该特定图像的 URL,然后使用imghtml 标记在页面上显示它。

This however doesn't get the alt, title, captionand descriptionof the image.

然而,这并不得到alttitlecaptiondescription图像。

The imgis not attached to a post or page field and so i assume you cannot use the Get Attachment function etc.

img没有连接到一个帖子或网页领域,所以我想你不能使用Get附加功能等。

The reason I want to use a function instead of writing out a static imghtml code is so that they are cached better and easier to maintain with all data for the image been updated in the Media Library instead of having to edit html code which is not idea for the end user.

我想使用函数而不是写出静态imghtml 代码的原因是为了更好地缓存它们并且更容易维护图像的所有数据都在媒体库中更新,而不必编辑 html 代码,这不是想法对于最终用户。

thank you in advance.

先感谢您。

采纳答案by Behnam Mohammadi

first get image

首先获取图像

function get_images_from_media_library() {
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' =>'image',
        'post_status' => 'inherit',
        'posts_per_page' => 5,
        'orderby' => 'rand'
    );
    $query_images = new WP_Query( $args );
    $images = array();
    foreach ( $query_images->posts as $image) {
        $images[]= $image->guid;
    }
    return $images;
}

and display image

并显示图像

function display_images_from_media_library() {

    $imgs = get_images_from_media_library();
    $html = '<div id="media-gallery">';

    foreach($imgs as $img) {

        $html .= '<img src="' . $img . '" alt="" />';

    }

    $html .= '</div>';

    return $html;

}

and use php fire event

并使用 php fire 事件

<?php echo display_images_from_media_library(); ?>

or use this function

或使用此功能

<?php

if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , true, false, 'Link to image attachment' );
}

?>

回答by Matt.C

I presume you have an attachment ID? Have you tried using attachement functions?

我猜你有附件 ID?您是否尝试过使用附件功能?

From the codex:

来自法典:

Note that media items are also 'Posts' in their own right and can be displayed as such via the WordPress Template Hierarchy. Themes can make use of this to loop over media items or create galleries.

请注意,媒体项目本身也是“帖子”,可以通过 WordPress 模板层次结构显示。主题可以利用它来循环媒体项目或创建画廊。

The following functions should get you started:

以下功能应该可以帮助您入门:

you can retrieve the image src using: wp_get_attachment_image_src()

您可以使用以下方法检索图像 src:wp_get_attachment_image_src()

$img= wp_get_attachment_image_src($attachmentID, $imageSizeName); 

you can get the image caption using: get_post_field()

您可以使用以下方法获取图像标题:get_post_field()

get_post_field('post_excerpt', $attachmentID)

you can get the alt tag using: get_post_meta()

您可以使用以下方法获取 alt 标签:get_post_meta()

get_post_meta($attachmentID, '_wp_attachment_image_alt', true);

回答by stalinrajindian

Please try to below code:

请尝试以下代码:

<?php
      $attachmentID = 1875;
      $imageSizeName = "thumbnail";
      $img = wp_get_attachment_image_src($attachmentID, $imageSizeName);
      //print_r($img);
?>

<img src="<?php echo $img[0]; ?>" alt="image">