Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

IF.. post meta show… Conditional Tag Help…?

So, I would refactor this a bit:

<?php
$featured = ( has_post_thumbnail() ? get_post_thumbnail() : false );
$first_img = false;
$custom = false;

if ( ! $featured ) {
    // No featured image; look for attached images and get the first one
    $attachments = get_children( 'post_type=attachment&orderby=menu_order&post_mime_type=image&post_parent=".$post->ID ); 
    $first_img = ( $attachments ? wp_get_attachment_image_src($attachments[0]->ID, "thumbnail-size') : false );
    // No attached images; look for custom-field image
    if ( ! $first_img ) {
        $custom_meta = ( get_post_custom($post->ID) ? get_post_custom($post->ID) : false );
        $custom = ( isset( $custom_meta['custom_field_name'][0] ) ? $custom['custom_field_name'][0] : "false" );
    }
}
?>

So, now, you can just step through each source; e.g.:

<?php
if ( $featured ) {
    // Post has a featured image; do something with it
} else if ( $first_img ) {
    // Post has attached image; do something with the first one
} else if ( $custom ) {
    // Post has image attached via custom field; do something with it
} else {
    // Otherwise, there's no featured image,
    // no attached images, and no image 
    // defined in post custom meta. Do something.
}
?>

You just need to fit in your markup accordingly.

Related Posts:

  1. Order by meta value or date?
  2. Filtering posts by post meta data
  3. Custom query with query_posts doesn’t show post without certain meta_key
  4. How to sort by meta value?
  5. get_post_meta causes database queries
  6. Querying posts with meta value that begins with a certain pattern
  7. order posts by meta value on posts page
  8. Query_post($args)
  9. query_posts() on key’s value, or key’s existence
  10. How to retrieve an array of post IDs by a particular value stored in a custom meta’s array
  11. Adding conditional content to single post-type template
  12. Query posts by meta_key whose value is an array
  13. Exclude posts by post meta value
  14. Frontend form with multiple posts
  15. query_posts orderby postmeta [closed]
  16. Ordering Posts By Meta Data
  17. get all posts with certain meta data
  18. Limiting the amount of posts this displays
  19. How to Get All Posts with any post status?
  20. posts_per_page no limit
  21. what is the correct way to compare dates in a WP query_posts meta_query
  22. How can i get count from query post
  23. query_post by title?
  24. Why query_posts() isn’t marked as deprecated?
  25. Alternative to query_posts for main loop? [duplicate]
  26. how to query posts by category and tag?
  27. Using WP_Query to Query Multiple Categories with Limited Posts Per Category?
  28. How do I query by post format in WordPress 3.1
  29. Display posts of the last 7 days
  30. Is there a way to exclude the content from the post variable to save on RAM usage?
  31. How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?
  32. Query posts by custom taxonomy ID
  33. Display/query post formats
  34. How to query post by user role?
  35. How to return results of a get_posts() in explicitly defined order
  36. Help to condense/optimize some working code
  37. How to set posts per page using WP_Query()
  38. query_posts exclude a meta key
  39. How do I create a random post that will last for a day
  40. Ensuring sticky posts are retrieved first (without using two queries)?
  41. Determine if more posts are available than was asked for in `query_posts()`?
  42. Modify main WordPress loop with a parse_query filter
  43. query_posts() in function makes global $wp_query out of sync?
  44. Limiting query_posts to 1, regardless of sticky post?
  45. How to make “sticky” pages (and query by them)
  46. Query posts: how to exclude results if post is in multiple categories
  47. Query Custom Meta Value with Increment
  48. query_posts ->using meta_compare / where meta value is smaller or greater or equals
  49. Problem with ‘post__not_in’
  50. How to order posts by descending comment count on taxonomy page?
  51. Sort posts alphabetically by custom field value, insert divider between different letters
  52. Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?
  53. WP 5.8 “Query Loop” block: where to place custom query?
  54. Sort X categories by last update and show image
  55. Custom Post Type “Event”: chronological list of recurring events
  56. using post__in allow duplicate post id
  57. Pagination on archive.php page
  58. WordPress Custom Query
  59. Best way to load page content in Fancybox popup?
  60. How should I intercept the main query and inject custom join / order by / group by criteria
  61. advice on creating a ‘related posts’ query like the one used on stackexchange
  62. order post my meta value m/d/y format with year as included value
  63. Order by meta value, pro first, then free
  64. Having trouble generating pagination links on custom query
  65. How to create an attachments archive with working pagination?
  66. _wp_page_template to dynamically use template
  67. Using dynamic conditions in ‘posts_where’ filter
  68. Help altering a query to exclude all but standard post format
  69. List users with the most total posts view
  70. Schedule Sticky Posts
  71. Use union/intersection query_posts variables in uri request parameter form?
  72. query_posts doesn’t order by title
  73. How to limit search to first letter of title?
  74. query_posts and pagination, still stuck after much research
  75. Query posts by taxonomy term name
  76. Sorting problem with ‘query_posts’ funcion in wordpress. Sort by custom field not working
  77. Total Count of Posts NOT in Selected Categories?
  78. how could I get the pagination as I want to when query posts using get_posts function
  79. Counter code for paginated category pages in wordpress
  80. Post Title displaying but not in the wrapped HTML I need
  81. Author List page: Exclude based on last post date
  82. WordPress Search Filter Only for Page with Child of Child of Child of Child of Child
  83. Replace query_posts with pre_get_posts
  84. query_posts sort in multiple directions
  85. Filter query_posts by tag slug on “Tag Archive” page (when tag is 2 or more words)
  86. pagination in author.php template returns 404 error
  87. How to display liked posts of current user in wordpress?
  88. Problem with single-page for my custom post
  89. How to ensure that is_search() return false after query_posts
  90. Warning: urlencode() expects parameter 1 to be string, array given in and not getting the preffered output
  91. How to modify the query to exclude posts by slug?
  92. query recent posts from several categories
  93. Inserting Post Using wp_insert_post. How to Fill Yoast Plugin SEO Fields
  94. Show Posts From Same Category OR Same Tag
  95. Post count per (day/month/year) since blog began
  96. Example Query Posts Showing the Latest Post with the Featured Image
  97. Dynamically excluding current page id
  98. Why is that gdsr_sort=thumbs (GD start rating plugin) only works with query_posts and not with WP_Query?
  99. How do i paginate query_posts with numeric pagination?
  100. Query_posts $query_string
Categories query-posts Tags conditional-tags, post-meta, query-posts
Show top commenters without using a plugin
wordpress threaded comments – customize nested ul / how child ul is displayd

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress