john
john

Reputation: 13

I can't get my if / else statement to display the_header_image_tag or not . Wordpress

I'm trying to get my if / else to properly work. I have a function called '' from WordPress. And I want to display different css if it is not used or is used.

Like my theme has a option to display a banner and if it is empty it doesn't display. But if you have it set, it will display the image.

From my understanding if the function is empty or not true it will not display anything. Thank You for your time.

The function on WordPress.org the_header_image_tag

Code

<style type="text/css">

<?php
endif; ?>

<?php
if ( ! the_header_image_tag() )
// Also tried if ( ! function_exists('the_header_image_tag')):
// Also tried if ( the_header_image_tag() )
?>
.site-title a {
    color: #<?php
echo esc_attr($header_text_color); ?>;
    position: relative;
}
.site-description {
    color: #<?php
echo esc_attr($header_text_color); ?>;
    position: relative;
} 

<?php
else: ?>
.site-title a {
    color: #<?php
    echo esc_attr($header_text_color); ?>;
    position: absolute;
    top: 150px;
    right: 100px;               
    display: block;
    z-index: 100;
}
.site-description {
   color: #<?php
   echo esc_attr($header_text_color); ?>;
   position: absolute;
   top: 20px;               
   display: block;
   z-index: 100;
}

</style>
<?php
endif; ?>

Upvotes: 1

Views: 112

Answers (2)

Victoria Ruiz
Victoria Ruiz

Reputation: 5013

In general, functions starting with the_ in Wordpress echo a string. So you can use <?php the_title(); ?> and it just displays it.

If you need the string returned, most of the functions have a counterpart starting in get_.

Using get_header_image_tag() instead should work inside your if statement.

Upvotes: 1

Bal&#225;zs Varga
Bal&#225;zs Varga

Reputation: 1856

The Codex say it will return an empty string if not set, so try checkink that. Also, you don't echo out your style closing tag at the end of your if statement, so i moved it outside the if-else.

<?php
endif; ?>

<?php
if ( the_header_image_tag() == '' ) {
// Codex say it will return an empty string if not used
// I also more like curly brackets (even if WP doesn't), but i don't think it will affect the results
?>
.site-title a {
    color: #<?php
echo esc_attr($header_text_color); ?>;
    position: relative;
}
.site-description {
    color: #<?php
echo esc_attr($header_text_color); ?>;
    position: relative;
} 

<?php } else { ?>
.site-title a {
    color: #<?php
    echo esc_attr($header_text_color); ?>;
    position: absolute;
    top: 150px;
    right: 100px;               
    display: block;
    z-index: 100;
}
.site-description {
   color: #<?php
   echo esc_attr($header_text_color); ?>;
   position: absolute;
   top: 20px;               
   display: block;
   z-index: 100;
}
// style closing tag moved outside. Your problem might be as simple as you don't echo out the closing tag in your if statement but only in the else
<?php } ?>
</style>

Upvotes: 0

Related Questions