I use a standard loop in my index.php and that shows all the posts beeing posted. What i'm looking for is a code that only shows the posts that have image(s) in it.
With other words: i only want to show posts that have images in content in my loop.
I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.
<?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;
?>
I think this just looks for images entered in the rich text editor - if you want to check for featured images as well you'll likely have to add something. Check out has_post_thumbnail
.
$searchimages
variable to allow for this.
I would suggest, that the easiest way to go about this is to create a special category called for example, image-post
or something to that effect and assign your post to that category and of course any other applicable categories in which it also may apply to.
Then you can create a loop which queries your image-post
category, thus it will show only those posts in which have been assigned to this special category. In effect producing the result you require.
Depending on your needs you can do this other ways, for example by using either..
Custom fields
This allows you to create a custom field meta_key/meta_value which you can use to signify the posts special condition, then you can query posts via meta_key/meta_value.
Custom Meta-Boxes (for advanced customization of your custom fields)
This allows you to give some give some better control over the display and use of custom_fields, essentially opening up access to a variety of UI controls (buttons, fields, text areas, select boxes, drop downs, radio buttons, checkboxes and more) for which you can control certain meta_values to your desire. Think of this as something that sits atop your custom_fields.
Use a custom post type especially for these types of posts
With custom post types, you can create a post type just for your image posts, you might call it something like Visuals
or whatever you like. They operate just like Posts and Pages do but are custom to your needs.
There might be a plugin out there that helps you achieve similar results too but since I rarely if ever use plugins (unless forced to) I wouldn't know. In any case I think a plugin would be overkill too in this situation.