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.

I found this code online. Its a code that only shows posts with images inside. Now, the problem is that if you set Wordpress to show 50 posts, this code shows the posts with the images, but also counts the ones without.

With other words.. if you got 50 posts on your frontpage where 6 are with images this code shows only 6 posts.

Does anyone know how I can make it show 50 posts (the same as set in WordPress) with images?

<?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; 
?>
share|improve this question
add comment

1 Answer

I haven't tried it, but I think that one proper solution could be to write a simple function that runs after the post is published (on save) and checks wether this post has images inside or not.If it has maybe is good to update a custom field in the post that will be a boolean and will check if it this post has images or not.This way you can do a WP Query afterwards and get the posts with images using a meta_key query.

share|improve this answer
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.