Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This script makes a loop showing only posts with images, but I have a problem. It doesnt show the right ammount of posts pr page. Probably because it counts the total nr of posts, including the ones without pictures. Does anyone know how to fix this?

<?php 
while ( have_posts() ) : the_post();
    $content = $post->post_content;
    $searchimages = '~<img [^>]* />~';
    preg_match_all( $searchimages, $content, $pics );
    $iNumberOfPics = count($pics[0]);
    if ( $iNumberOfPics > 0 ) {
        //your loop content goes here
    } 
endwhile; 
?>

Second problem: It also shows broken-image if the image in the post is external.

share|improve this question
add comment

1 Answer

I think that the only way is to query the database with LIKE statement and get all IDs and then create a separate WP_Query object for the list of post ID's. Check this guide for example.

share|improve this answer
    
I'm sorry Mario but my knowlage is not there yet :( can you help? –  Demilio May 29 '12 at 7:28
    
it's a bit of manual work, I'm personally using the Views WP plugin that does a lot of querying and has pagination parameters –  Mario Peshev May 30 '12 at 11:27
    
ok, thanks for the tip :) –  Demilio May 31 '12 at 8:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.