/    Sign up×
Community /Pin to ProfileBookmark

Adding an If Statement to a piece of code

Hi there,

I need to add the following piece of code to a WordPress site…

<img src=”<?php header_image(); ?>” height=”<?php echo get_custom_header()->height; ?>” width=”<?php echo get_custom_header()->width; ?>” alt=”image description” class=”custom_header” />

…however, I want to amend it for instances where no image exists – i.e., if there’s no image, show nothing.

Can someone please help?

Thank you.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 01.2014 — How can you tell if there is an image or not? That will be the only "tricky" part. Once we know that, a simple IF clause should be sufficient.
Copy linkTweet thisAlerts:
@riz-manauthorOct 01.2014 — Hi, thanks for responding.

I'm using a plugin called Unique Headers. It allows me to upload an associating image for each category and by using the code above, I can then have it displayed with the category heading. The code that controls this is:

// Add support for flexible headers

$header_args = array(

'flex-height' => true,

'height' => 183,

'flex-width' => true,

'width' => 680,

'default-image' => get_template_directory_uri() . '/images/header.jpg',

// 'admin-head-callback' => 'mytheme_admin_header_style',

);

add_theme_support( 'custom-header', $header_args );

The problem is, if I have not uploaded an image for a given category, the code, as it stands, will end up displaying a broken image with the text based category heading (I don't wish to use the default image option and haven't actually uploaded an image called header.jpg). Is this where you'd add an IF Statement?
Copy linkTweet thisAlerts:
@NogDogOct 01.2014 — If nothing "nicer", maybe you could just do something like:
[code=php]
<?php
$image = header_image();
if($image != false) {
?>
<img src="<?php echo $image ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="image description" class="custom_header" />
<?php } ?>
[/code]
Copy linkTweet thisAlerts:
@riz-manauthorOct 01.2014 — Thank you for your time.

While that now stops broken images appearing on category pages that have no images, for categories that should have an image showing, I just see the image's URL - not the actual image.

Any ideas what's gone wrong?
Copy linkTweet thisAlerts:
@riz-manauthorOct 01.2014 — On further investigation...it seems that the...

<img src="<?php echo $image ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="image description" class="custom_header" />

...bit of your code isn't doing anything. If I remove it, I still get the actual URL of the image showing where the actual image is supposed to be.

Moreover, isn't...

$image = header_image();

if($image != false)

...bit of your code supposed to be part of...

// Add support for flexible headers

$header_args = array(

'flex-height' => true,

'height' => 183,

'flex-width' => true,

'width' => 680,

'default-image' => get_template_directory_uri() . '/images/header.jpg',

// 'admin-head-callback' => 'mytheme_admin_header_style',

);

add_theme_support( 'custom-header', $header_args );

Bearing in mind I know very little about PHP, doesn't it make sense to combine these two?

E.g., if there's no image...show nothing but if there is...then make sure $header_args applies when displaying the image.
Copy linkTweet thisAlerts:
@NogDogOct 02.2014 — I suspect that the header_image() function directly echoes out its result instead of returning a value (ugh...not a good coding pardigm, IMHO). Which then takes me back to my first reply, questioning how the code at that point can "know" whether or not there is an image, which is what I was trying to do. For example, is there a get_header_image() function, perhaps, that just returns the URL instead of automatically outputting it?
Copy linkTweet thisAlerts:
@riz-manauthorOct 02.2014 — Hi thanks for sticking with me on this.

To be honest, this is way above my knowledge levels :o but I've gone through the Wordpress files and found a file called image.php. I've looked through it and found these..

wp_get_attachment_url( get_the_ID() )

wp_get_attachment_image( get_the_ID())

I can post the coding for the entire page if that helps - it's not a large file.
Copy linkTweet thisAlerts:
@NogDogOct 02.2014 — A total kludge would be to capture the output of the get_header() function:
[code=php]
<?php
ob_flush(); // this may not be needed, but shouldn't hurt
ob_start();
header_image();
$image = trim(ob_get_contents());
ob_end_clean();
if($image !== '') {
?>
<img src="<?php echo $image ?>" height="<?php echo get_custom_header()->height; ?>"
width="<?php echo get_custom_header()->width; ?>" alt="image description" class="custom_header" />
<?php } ?>
[/code]

Maybe ?
Copy linkTweet thisAlerts:
@riz-manauthorOct 02.2014 — No may be's about it - that did the trick. I wish I knew what it all means...but oh well. Thank you very much.

Great stuff!
×

Success!

Help @riz-man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...