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

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

问题描述

我有一些我在某个博客上找到的代码,应该显示来自WordPress帖子的所有图片。

 函数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;
}

虽然...会正确显示第一张和第二张图片,第二张图片,而不是第三张第四张等等。它抓取的图片数量还可以,但是不是显示第1,2,3,4张图片,它会显示第一,第二,第二,第二...



任何人都可以看这段代码,也许会想出为什么会发生这种情况?我知道代码是相当sl,的,但我只是在一些博客上发现它,是一个PHP新手和所有:)

所有的帮助表示感谢,提前感谢!

p>

解决方案

  $ 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);
//您的代码在这里
}

你也可以在这里看看:
http://www.rlmseo.com/blog / get-images-attached-to-post /

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...

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
}

Also you can have a look here: http://www.rlmseo.com/blog/get-images-attached-to-post/

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

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