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?
- WordPress Post Query Using Custom native MySQL like St_Distance_Sphere
- Get post category as a separate string and url
- Have h2 tag not show up if there are no blog posts
- Link post images to post
- Limiting the amount of posts retrieved by the loop
- Trying to query all posts in category 4
- Getting posts from some categories plus some individual posts
- Pagination for normal (standard) posts on a page with a custom loop?
- How to get the next and previous image title from attachment or gallery?
- Changing wp_link_pages() to “Next Page” and “Previous Page” buttons?
- Attachment image single page
- I can’t find the relevant part of the loop in my theme file. How do I find the full content with “More…”
- Get posts from current category?
- How to change the order of elements within a post?
- Only show featured image on exerpt and exclude images in post
- posts going to 2 pages/categories only show up in 1
- How to repeat a loop after 4 posts [duplicate]
- Custom Post Type Single Loop Outputting Wrong Post
- Using the loop to set locations for all posts on a single google map
- Change content off every sixth element
- How to separate each individual blog post?
- Making a wordpress page print friendly
- How to call WordPress First Image Post or else Attached Image or Default
- Regarding Posts
- how can display a post on home screen without images
- Defining ‘last’ class on foreach blog posts
- How to assign a post to a post parrent?
- Get current taxonomy and display query accordingly
- query for filtering published posts?
- How to show full post on home page
- Help on conditional statement to accompany wp_insert_post function please?
- get parent fields title, content excerpt etc
- How to implement a WordPress comments function?
- Error while excluding a post from another loop (using its ID)
- Is there a way to define the $post var outside the loop?
- How to add content above footer in posts from specific category
- Changing The Default Header POST Title and Description text by category in wordpress
- Set Default Post as Parent Child (Nested) Relation
- AWS usage queries
- Pull in posts using post meta for REST API
- Move position in post list for a custom checkbox column
- I need to create “View Profile” and “Edit Profile” links by incorporating the Post Title/ID, but can’t seem to manage it