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?
- WordPress current post functions inside loop doesn’t work anymore, are they incompatible with php 8.0?
- Why does ‘get_the_ID’ work outside of the loop?
- List only child categories a post is in, of a specific parent category
- Unattaching images from a post
- How to change post status from publish to draft using hook in wordpress?
- Alternate custom content in the loop
- Auto Refresh Post List after X seconds
- Display posts with comments closed, with pagination?
- Remove permalink from images when inserting into post
- Featured Image uploaded, attached and set but only appearing in the media library
- wp-cli post create & media import issues
- Multi row post list
- wp_query random post
- Possible to alternate between two loops
- Get Value of Post before
- Counter is skipping post when I still want it visible [closed]
- Every second post different class in blog view
- How to give classname to post if post has no content?
- Images showing up in Post Editor, but not in Published Post
- How to retrieve certain number of images from a wordpress post?
- WordPress post filter menu
- Some doubts about how the loop work (trying debugging it)
- Change the background of each post according to the category
- Same post appears in related Posts?
- Get post category as a separate string and url
- How to show full post on home page
- Pull in posts using post meta for REST API
- ACF flexible content block not showing on live site (works locally)
- get category in list of posts // shortcode for custom related posts
- Alt text attributes not showing over portfolio images
- Set a post expiration and delete a post when expirate
- Retrieve posts inside foundation tabs and tab-content split by 3 per row
- Make recent post display recent updated, instead of recent published
- Post being duplicated with foreach loop
- Custom post type – Loop out in random order but same 15 to appear first… still random
- WordPress infinite post cycle
- Show only top 3 posts from 3 categories in order on home page
- How do I control the fallback query after the original query returned cero posts?
- WP Gallery Image Page Issue When 2 galleries share one image
- The loop: more posts than I want to fit on one page
- display last post modified date in genesis child themes
- Count words for all posts by all authors
- Word count for all posts of all authors
- Optimize blog that serves hundred of images and videos