显示WordPress帖子中的所有图像

时间:2010-11-02 19:46:35

标签: php image wordpress function

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

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

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

任何人都可以看一下这个片段,也许会想出为什么会这样吗?我知道代码相当邋,,但我只是在一些博客上找到它,作为一个PHP新手和所有:)

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

5 个答案:

答案 0 :(得分:8)

$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-attached-to-post/

答案 1 :(得分:4)

现在使用新的wordpress get_attached_media($ type,$ post)功能

$attachments= get_attached_media( 'image', $post->ID );
foreach($attachments as $att_id => $attachment) {
  $full_img_url = wp_get_attachment_url($attachment->ID);
// You can echo it out here
}

注意,这只会将文件上传添加到帖子中。不是通过媒体库添加的文件。

答案 2 :(得分:1)

试试这个!它可能有用。

function getImage() {
    global $more;
    $more = 1;
    $link = get_permalink();
    $content = get_the_content();
    $count = substr_count($content, '<img');

    for($i=1;$i<=$count;$i++) { 
        //move $start = 0 inside the loop
        $start = 0;
        $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; }
        $content = substr($content,$imgEnd+1);
    }
    $more = 0;
}

答案 3 :(得分:0)

<?php
    if ( have_posts() )
    while ( have_posts() ):
    the_post();

    $args = array(
        'post_type'   => 'attachment',
        'numberposts' => -1,
        'post_parent' => $post->ID
        );

    $attachments = get_posts( $args );

    if ( $attachments )
    {
        foreach ( $attachments as $attachment )
        {

        echo wp_get_attachment_image( $attachment->ID, false );

        }
    }

    endwhile;
?>

来源:http://960development.com/code-snippet/get-all-the-images-attached-with-a-wordpress-post/

答案 4 :(得分:0)

已修复,您需要添加$ imgLength。

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);
        $imgLength = strpos($post, '>');
        $imgEnd = $imgBeg + $imgLength;
        $postOutput = substr($post, 0, $imgLength+1);
        $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
        if(stristr($postOutput,'<img')) {    
            echo $postOutput;
        }
        $start=$imgEnd+1;
    }
    $more = 0;
}