siouxfan45
siouxfan45

Reputation: 199

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>

Upvotes: 1

Views: 529

Answers (3)

kasper Taeymans
kasper Taeymans

Reputation: 7026

You can try this:

if (get_option('to_logo') == '') {
    echo bloginfo('name');
} else {
    echo get_option('to_logo');
}

Upvotes: 0

Allen Liu
Allen Liu

Reputation: 4038

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>

Upvotes: 1

Jared Farrish
Jared Farrish

Reputation: 49208

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>

Upvotes: 3

Related Questions