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
.
Related Posts:
- How to Display Post View Count
- How to get posts published between a date and today?
- disable WP automatically inserted line breaks after an image
- Display all posts from specific categories on a page
- How to exclude latest x posts from a paginated query?
- Display content from a specific category
- How do I use element instead of tags in WordPress post content having webP support?
- Can’t show comments count per post outside loop
- Limit function to specific post category
- Function to show random posts from a category
- Hiding posts in a list from specified categories
- How would I get 1 latest post from a query for 5 posts?
- Converting a dynamic piece of code using WordPress Loop into a static one using Post ID
- Inserting custom data to the_post() during loop
- How To Get Posts With A Specific Word
- Load post attached images on a single page site with fancybox
- Why WP_Query(‘showposts=5’) shows only 1 post?
- Display all posts that were published before full post on single.php
- Exclude category from
- Blog post per page setting conflicting with custom WP_Query?
- How to exclude latest x posts from a paginated query?
- Two loops, one AJAX loop, exclude posts in first loop from second loop, loops are in different files
- Order posts alphabetically: how to set order=asc in mysql query?
- How to order posts in an arbitrary order?
- Display Posts on Custom Page
- Wrong post title displayed from loop
- Change all author links in Blog roll
- Get the amount of posts on a current page
- How to get posts published on the latest date?
- The_excerpt() doesn’t parse – how to change that?
- Add custom text automatically on each post
- How to check in functions.php if there is data in a WP_Query?
- WP_Query: Show 10 posts in date order, first three random
- Get post by tag
- Query function not executed between element [closed]
- Optimize CPT-function with a loop
- Display specific posts on home page
- Display 3 posts with different HTML markup using a loop
- WordPress as CMS: How to manage/handle images assigned to a post as full-sized background images in a slideshow?
- How to show post views shortcode data for each post on Posts List Page?
- How to show category image if no featured image is set?
- Get post id outside loop : Notice: Trying to get property of non-object
- Show post excerpt
- Show author bio box
- How to correctly escape data
- Use special template for the first post in the loop
- Modify WordPress loop after it has been run
- show latest authors blog post
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- Adding a category at even positions on main loop with modified pagination
- How to limit the number of results for all query_posts on mysite
- How to inject a post within a loop
- Getting rid of the blog page entirely
- Only show posts with image and the correct ammount of posts in loop
- wp trim function not working
- What function does the loop of displaying posts?
- Should we trust the post globals?
- How to call function within a page/post, to dynamically generate content?
- Count singular post views automatically
- Looping through posts per category gives same posts for each category
- How to have different content in the loop and single
- Guest Author – How to modify my custom function code if the guest author URL will follow a particular pattern/format?
- Only display posts with comments
- Add confirmation popup on “Move to Trash”
- Divide Loop Into Days & Categories
- Can I set and show “important” post in my blog?
- How to display different number of posts
- WP Query – duplicated posts once including tags in search results
- Alternate row and columns every X posts
- show first 3 thumbnails of posts in different sizes [closed]
- Homepage’s content is dependent on the custom field values (set automatically), how do I get homepage to update without manually updating page?
- wp_posts table: safely remove unused columns to save database storage
- Multiple Loops Breaking Pages
- Why this function doesn’t works without the_post?
- Posts page not showing correct image
- wp_query with ajax
- Get postlist for each category using query_posts?
- How to save posts position on homepage after random function?
- Help on conditional statement to accompany wp_insert_post function please?
- Site/Post preview is not working on all social media
- Delete media from the post but keep my featured image and post
- Thumbnails and images in the blog post are not loading
- Numbered Pagination Showing The Same Posts After Altering WP_Query
- Count singular post views automatically
- Loop and output 4 rows of posts on home page
- Loop through all posts showing duplicates
- Manipulate query to show specific post
- Embed image in post from external url
- How to change number of posts shown on homepage vs other pages?
- Is possible to do this from function.php
- How to show the posts list into a static page? Problems to use the loop into a static page
- Get post title by Alphabet
- wordpress taxonomy results
- How can I create a menu items from meta box based on users input
- How do I show the post title if an advanced custom field hasn’t been used?
- Custom post order returning posts from other categories
- Custom Post-Rename Function Does Not Function in WordPress 6.x
- Import alt text in set_post_thumbnail
- Sort Posts Alphabetically Based on Specific Category (Divi)
- How to sort posts alphabetically based on a specific parent category