显示 WordPress 帖子中的所有图像 [英] Displaying all images from a WordPress post

查看:28
本文介绍了显示 WordPress 帖子中的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某个博客上找到了这段代码,它应该显示 WordPress 帖子中的所有图像.

I have this piece of code I found on some blog, that's supposed to display all images from a WordPress post.

function getImage() {
    global $more;
    $more = 1;
    $link = get_permalink();
    $content = get_the_content();
    $count = substr_count($content, '<img');
    $start = 0;
    for($i=1;$i<=$count;$i++) {
        $imgBeg = strpos($content, '<img', $start);
        $post = substr($content, $imgBeg);
        $imgEnd = strpos($post, '>');
        $postOutput = substr($post, 0, $imgEnd+1);
        $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
        if(stristr($postOutput,'<img')) { echo $postOutput; }
        $start=$imgEnd+1;
    }
    $more = 0;
}

虽然会发生什么...它正确显示第一张和第二张图像,然后循环第二张图像而不是第三张第四张等等.它可以抓取图像的数量,但不是显示第一张、第二张、第三张、第四张图像,而是显示第一、第二、第二、第二...

What happens though... it displays first and second image correctly, then loops the second image instead of 3rd 4th etc. It grabs the number of images okay, but instead of displaying 1st, 2nd, 3rd, 4th image, it displays 1st, 2nd, 2nd, 2nd...

有人可以看看这个片段并想出为什么会发生这种情况吗?我知道代码相当草率,但我只是在一些博客上找到它,作为 PHP 新手和所有 :)

Could anyone look at this snippet and maybe come up with an idea why this happens? I know the code is rather sloppy, but I just found it on some blog, being a PHP newbie and all :)

感谢所有帮助,提前致谢!

All help appreciated, thanks in advance!

推荐答案

$attachments = get_children(array('post_parent' => $post->ID,
                        'post_status' => 'inherit',
                        'post_type' => 'attachment',
                        'post_mime_type' => 'image',
                        'order' => 'ASC',
                        'orderby' => 'menu_order ID'));

foreach($attachments as $att_id => $attachment) {
    $full_img_url = wp_get_attachment_url($attachment->ID);
    // Your Code here
}

你也可以看看这里:http://www.rlmseo.com/blog/get-images-附加到帖子/

这篇关于显示 WordPress 帖子中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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