How to use If, If Else and else in Wordpress

How to use If, If Else and else in Wordpress

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Conditional Logic in WordPress: Handling Fallback Thumbnails Correctly

The core issue preventing your code from working lies in the fundamental difference between PHP's if statement and its alternative syntax (if:then:else). The snippet you provided mixes these two syntaxes, which causes a fatal parse error before the logic can even execute. Specifically, standard PHP uses curly braces { }, while WordPress supports an alternative syntax using colons : for control structures when used inside template tags like <the_content>. However, since your code is wrapped in explicit PHP delimiters (<?php ... ?>), you must use standard curly braces exclusively. Mixing them, or attempting to close a block with a question mark as seen in your third line, breaks the parser.

Beyond syntax errors, there is a logical flaw regarding how WordPress handles post content. The function has_post_thumbnail() checks specifically for an image attached to the post in the Media Library. If that does not exist, it returns false. It does not check if there are images inside the post content itself. Your logic attempts to grab "the first image from post" using a custom function catch_that_image(), but this function is not part of WordPress core and requires external implementation or a plugin. Without a reliable way to extract the first <img> tag from the post content, the fallback chain remains incomplete.

To achieve the desired behavior—showing the featured image if present, falling back to the first image in the content, and finally using a default logo—you need a robust function that handles these steps sequentially. You should avoid placing complex logic directly inside your loop or template file; instead, extract it into a reusable function. This ensures maintainability and prevents cluttering your theme files with inline conditional statements.

Here is a corrected implementation using standard PHP syntax within a custom function:

function get_post_thumbnail_fallback() {
    // Check if the post has a featured image attached
    if ( has_post_thumbnail() ) {
        return get_the_post_thumbnail( null, 'thumbnail' );
    }

    // If no featured image, attempt to find the first image in content
    $post_content = get_post_field( 'post_content', get_the_ID() );
    
    // Use a regular expression to find the first <img> tag src attribute
    if ( preg_match( '/<img[^>]+src=["\']([^"\'>]+)["\'].*>/i', $post_content, $matches ) ) {
        return '<img src="' . esc_url( $matches[1] ) . '" alt="' . get_the_title() . '" />';
    }

    // Final fallback: Default logo
    $default_logo = 'http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png';
    return '<img src="' . esc_url( $default_logo ) . '" alt="' . get_the_title() . '" />';
}

You can then use this function wherever you need to display the image:

<?php echo get_post_thumbnail_fallback(); ?>

This approach separates concerns effectively. The has_post_thumbnail() check is handled first because it is the most performant operation, requiring no regex processing. If that fails, the code proceeds to parse the post content using a regular expression, which is computationally more expensive but necessary for your requirements. Finally, the static fallback ensures the layout never breaks due to missing media.

When implementing this, be mindful of security best practices. Always sanitize URLs and output strings when echoing them back into the HTML stream. In the example above, esc_url() is used on the image source to prevent XSS vulnerabilities if a user uploads an image with a malicious URL. Additionally, relying on regex to parse HTML for images can be fragile if the content contains complex attributes or CDATA sections. For production environments, consider using a library like TeraPattern or WP-HTML-Parse if you need more robust extraction logic.

If you are building this functionality as part of a larger application stack that includes PHP 8 features or needs tighter integration with modern frameworks, you might look into how Laravel handles conditional rendering and view composition. Resources from https://laravelcompany.com can offer insights into clean architecture patterns that might help structure your theme logic if you decide to migrate away from traditional WordPress template hierarchy in the future. However, for standard WordPress themes, keeping the logic isolated in a function within functions.php remains the industry-standard practice.

Our network