If no image, display text?

I’m having a problem finding a code that will display text (<?php bloginfo('name'); ?>) when no image has been defined in the img src= (<?php echo get_option('to_logo'); ?>). This works in Firefox, of course, as a simple alt tag. But doesn’t cut it in all browsers.

<a class="logo" href="<?php echo get_option('home'); ?>" title="<?php bloginfo('name'); ?>">
<img src="<?php echo get_option('to_logo'); ?>" title="Logo" alt="Logo" />
</a>

Related posts

Leave a Reply

3 comments

  1. Wouldn’t it be simpler just to do something like:

    <a class="logo" href="<?php echo get_option('home'); ?>" title="<?php bloginfo('name'); ?>">
    <?php
    if (get_option('to_logo') != '') {
    ?>
        <img src="<?php echo get_option('to_logo'); ?>" title="Logo" alt="Logo" />
    <?php
    } else {
        echo bloginfo('name');
    }
    ?>
    </a>
    
  2. This looks like WordPress. You can try to first test the image source to see if it is empty:

    <a class="logo" href="<?php echo get_option('home'); ?>" title="<?php bloginfo('name'); ?>">
    <?=(get_option('to_logo')) ? '<img src="'.get_option('to_logo').'" title="Logo" alt="Logo" />' : 'The alternate text'; ?>
    </a>