0

If post content exists, do not display featured image, if it does not exists, then display featured image or url. So, basically I want featured image to be displayed only if nothing else in post content to be displayed. Thanks!!!!

3
  • 1
    What have you tried thus-far? You'll need to show effort on your part, not just your need for an answer.
    – Zach
    Commented Oct 23, 2012 at 20:12
  • I have this so far, it looks for the shorcodes and then displays them if found in the homepage, if not then featured image is displayed. Basically, I want featured image or anything else to be displayed if no other content, but if there is a content then I dont want image or url to be displayed, just a post content. Commented Oct 23, 2012 at 20:18
  • Here is the code so far: <?php $pattern = get_shortcode_regex(); $matches = array(); preg_match_all("/$pattern/s", get_the_content(), $matches); echo preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $matches[0][0] ); ?> Commented Oct 23, 2012 at 20:18

2 Answers 2

1

Just check the raw post content without any filters:

if ( "" === $post->post_content )
{
     the_post_thumbnail();
}
else
{
    the_content();
}
8
  • Do I do this in loop or function.php? Thanks Commented Oct 23, 2012 at 20:23
  • In a loop. You could encapsulate it in a function and move it to the functions.php but I think just using it as showed here will be easier to understand later.
    – fuxia
    Commented Oct 23, 2012 at 20:36
  • Your code works same as what I had: <?php $pattern = get_shortcode_regex(); $matches = array(); preg_match_all("/$pattern/s", get_the_content(), $matches); echo preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $matches[0][0] ); ?> . I just cant figure out how to display post content without featured image showing up? But, I do want the featured image to show up if nothing else in post. Thanks again.... Commented Oct 23, 2012 at 21:19
  • I am not sure I can follow. Could you try to edit your question to make that easier to understand?
    – fuxia
    Commented Oct 23, 2012 at 21:30
  • Sorry 4 that, your code works great, it displays whatever is in the post content. For example if I have a post with the post content in it and there is also a featured image there too, I want some kind of code to ignore the featured image and only to display the post content; on the other hand, if there is nothing in post to be displayed, then I want that featured image to be displayed. This is all done in the front page (homepage), so there is always going to be something to be displayed no matter if its post or featured image....Thanks Commented Oct 23, 2012 at 21:35
0

Inside the template file with the loop you'd want something like encased in PHP tags...

( empty( get_the_content() ) ) ? the_post_thumbnail() : the_content();
2
  • get_the_content() can be filled by plugins with extra strings and might not be empty even if the post_content is empty.
    – fuxia
    Commented Oct 23, 2012 at 21:00
  • That's a good point, I didn't think of that...thank you for the insight :)
    – dmarges
    Commented Oct 24, 2012 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.