从 wordpress 帖子中获取图片(帖子内容) [英] Get images from wordpress post (post content)

查看:40
本文介绍了从 wordpress 帖子中获取图片(帖子内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 wordpress 中获取帖子的第一张图片.我有各种各样的帖子.所以对于新帖子,我可以设置特色图片.然而,有成千上万的旧帖子.我需要从这些帖子中提取第一张图片,以便我可以使用它们进行显示.

I need to get the first image of the post in wordpress. I have various posts. So for new posts, I can set the featured image. However there are thousands of old posts. I need to extract the first image from those posts so that I can use them to display.

我使用了来自 http 的代码://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post/ 我不认为它对我有用.

I used the code from http://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post/ and I dont think its working for me.

global $post;
$args = array( 'posts_per_page' => 10, 'category' => 6 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
......
.....
endforeach;

我需要以缩略图的形式显示每个帖子中的图像,比如在画廊中.我搜索了很多,但不知道如何搜索.

I need to display the image from each post lets say as in a gallery in the form of thumbnails. I searched a lot but could not figure out how.

推荐答案

把它放在你的 functions.php

function getTheFirstImage() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
    if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0; $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', false);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $thumb=wp_get_attachment_thumb_url($num);
        echo "<img src='$thumb' class='thumbnail' />";
    endif;
}

然后在你的模板中使用 getTheFirstImage() 函数来打印图像

Then use in your template getTheFirstImage() function where you want to print the image

$args = array( 'posts_per_page' => 10, 'category' => 6 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
    getTheFirstImage(); // Will print the image
    ......
    .....
endforeach;

查看此论坛帖子.

这篇关于从 wordpress 帖子中获取图片(帖子内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆