WordPress 从帖子中获取第一张图片

发布于 12-15 06:01 字数 1150 浏览 2 评论 0原文

试图从帖子中获取第一张图片,但我的 php 代码没有返回任何内容,有什么帮助吗?

<?php while ($browndog_blog->have_posts()) : $browndog_blog->the_post();                    
    $args = array(
    'numberposts' => 1,
    'post_mime_type' => 'image',
   'post_parent' => $post->ID,
    'post_status' => null,
    'post_type' => 'attachment'
    );

    $attachments = get_children( $args );

    //print_r($attachments);

    if ($attachments) {
        foreach($attachments as $attachment) {
            $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );

            echo '<a href="'.get_permalink($post->ID).'"><img src="'.wp_get_attachment_thumb_url( $attachment->ID ).'"></a>';
            echo '<p>'.get_the_excerpt($post->ID).'</p>';
            echo '<p><a href="'.get_permalink($post->ID).'">Read More</a></p>';
        }
    }
endwhile; ?>

不知道出了什么问题,因为我使用类似的代码来获取所有图像附件,而不仅仅是一个,而且效果很好。

Trying to get the first image from a post, but my php code doesn't return anything, any help?

<?php while ($browndog_blog->have_posts()) : $browndog_blog->the_post();                    
    $args = array(
    'numberposts' => 1,
    'post_mime_type' => 'image',
   'post_parent' => $post->ID,
    'post_status' => null,
    'post_type' => 'attachment'
    );

    $attachments = get_children( $args );

    //print_r($attachments);

    if ($attachments) {
        foreach($attachments as $attachment) {
            $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );

            echo '<a href="'.get_permalink($post->ID).'"><img src="'.wp_get_attachment_thumb_url( $attachment->ID ).'"></a>';
            echo '<p>'.get_the_excerpt($post->ID).'</p>';
            echo '<p><a href="'.get_permalink($post->ID).'">Read More</a></p>';
        }
    }
endwhile; ?>

Not sure what's going wrong as I'm using a similar code to get all of the image attachments and not just one, and that works fine.

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

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

发布评论

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

评论(2

七度光2024-12-22 06:01:27

get_children() 仅返回已直接上传到该帖子的图像。如果图像已附加到给定帖子,它将不会被视为其子图像,因此不会由上述函数返回。

检查帖子子项的一个简单方法是登录仪表板并转到帖子、编辑帖子。单击编辑器上方的添加媒体按钮,然后从唯一的下拉框中选择上传到此帖子。如果为空,则无论帖子内容如何,​​get_children都不会返回任何图像。

get_children() only returns images that have been uploaded directly to that post. If an image has been attached to a given post it will not be considered one it's children and thus wont be returned by the afore mentioned function.

An easy way to check a post's children is login to the dashboard and go to posts, edit post. Click the Add Media button above the editor and from the only drop down box select Uploaded to this post. If this is empty then get_children will not return any images regardless of the post's content.

原来分手还会想你2024-12-22 06:01:27

我想我只是做了和你想做的一样的事情......我不会自称是专家,但这就是我为让它发挥作用所做的事情,如果幸运的话,你将能够根据你的需要调整它。

$image_id=get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,’large’);
$image_url=$image_url[0];

基本上,根据我的理解,数组中的第一个图像是缩略图,所以这就是为什么我首先获取缩略图 ID,然后用它来检索图像的大版本。

I think I just did the same thing as you are looking to do... I won't claim to be a guru, but here's what I did to get this working and with any luck you will be able to adapt it to your needs.

$image_id=get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,’large’);
$image_url=$image_url[0];

Basically the first image in the array is the thumbnail, as I understand it, so that's why I grab the thumbnail id first, then use it to retrieve the large version of the image.

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