If no image, display text? - php

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>

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>

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>

You can try this:
if (get_option('to_logo') == '') {
echo bloginfo('name');
} else {
echo get_option('to_logo');
}

Related

Display different logo in home page - wordpress

i would like to display a different logo in home page (with ACF).
i try with this code but it doesn't work :
<?php if(is_home()){ ?>
<?php $logo = get_field( 'logo', 'option' ); ?>
<?php if ( $logo ) { ?>
<a href="<?php echo home_url(); ?>" class="navbar-brand">
<img src="<?php echo $logo['url']; ?>" alt="" />
</a>
<?php } ?>
<?php } else { ?>
<?php $logo_footer = get_field( 'logo_footer', 'option' ); ?>
<?php if ( $logo_footer ) { ?>
<a href="<?php echo home_url(); ?>" class="navbar-brand">
<img src="<?php echo $logo_footer['url']; ?>" alt="" />
</a>
<?php } ?>
<?php } ?>
Do you have any tips ?
thank you
If you have set the homepage in Settings->Reading->Your homepage, you'll have to use is_front_page() instead of is_home().
More information here: https://wordpress.stackexchange.com/a/30389
Add This code into your php file where you want to add it.
i hope its working.
<?php
$logo = get_field( 'logo', 'option' );
$logo_footer = get_field( 'logo_footer', 'option' ); ?>
<?php if ( $logo ) { ?>
<a href="<?php echo home_url(); ?>" class="navbar-brand">
<img src="<?php echo $logo['url']; ?>" alt="" />
</a>
<?php } ?>
<?php if ( $logo_footer ) { ?>
<a href="<?php echo home_url(); ?>" class="navbar-brand">
<img src="<?php echo $logo_footer['url']; ?>" alt="" />
</a>
<?php } ?>

Specify Image Width and Height being called by PHP file

I am trying to figure out where to specify the image size of the facebook.png, which is 24x24.
Below is the code, but I am not sure how to specify the size. I know how to do this in HTML, but not in PHP.
<?php if ( get_option('solostream_facebook_url') ) { ?>
<a title="<?php echo stripslashes(get_option('solostream_facebook_link_text')); ?>" rel="external" href="http://www.facebook.com/<?php echo stripslashes(get_option('solostream_facebook_url')); ?>"><img class="facebook-sub" src="<?php bloginfo('template_directory'); ?>/images/facebook.png" alt="<?php echo stripslashes(get_option('solostream_facebook_link_text')); ?>" align="top" /></a>
<?php } ?>
Add the height and width attributes to <img>. Check below code.
<?php if ( get_option( 'solostream_facebook_url') ) { ?>
<a title="<?php echo stripslashes(get_option('solostream_facebook_link_text')); ?>" rel="external" href="http://www.facebook.com/<?php echo stripslashes(get_option('solostream_facebook_url')); ?>"><img class="facebook-sub" src="<?php bloginfo('template_directory'); ?>/images/facebook.png" alt="<?php echo stripslashes(get_option('solostream_facebook_link_text')); ?>" align="top" height="24" width="24"/></a>
<?php } ?>
Are you tryying to get PHP to retrieve the width / height?
If so, the getimagesize() function will do this:
<?php if ( get_option('solostream_facebook_url') ) { ?>
list($width, $height, $type, $dims) = getimagesize("bloginfo('template_directory')/images/facebook.png");
<a title="<?php echo stripslashes(get_option('solostream_facebook_link_text')); ?>" rel="external" href="http://www.facebook.com/<?php echo stripslashes(get_option('solostream_facebook_url')); ?>"><img $dims class="facebook-sub" src="<?php bloginfo('template_directory'); ?>/images/facebook.png" alt="<?php echo stripslashes(get_option('solostream_facebook_link_text')); ?>" align="top" /></a>
<?php } ?>

URL redirection depending on logged in or not

I have a logo and I want it to redirect to a homepage when the user is logged in or to a register site when the user is not logged in.
Here is my code:
<strong class="logo">
<?php if (is_user_logged_in()) ?>
<a href="http://test/register/">
<?php else ?>
<a href="<?php echo home_url();?>">
<img id="logo_img" title="<?php bloginfo('name'); ?>" src="<?php echo $logo_path; ?>" alt="<?php bloginfo('name'); ?>">
I'm using Wordpress. It says unexpected "else".
You are not doing your if / else statements correctly:
<strong class="logo">
<?php if (is_user_logged_in()) { ?>
<a href="http://test/register/">
<?php } else { ?>
<a href="<?php echo home_url();?>">
<?php } ?>
<img id="logo_img" title="<?php bloginfo('name'); ?>" src="<?php echo $logo_path; ?>" alt="<?php bloginfo('name'); ?>">
if you want to use this in a way for no brackets you have to do something like this:
<strong class="logo">
<?php
if (is_user_logged_in())
echo '<a href="http://test/register/">';
else
echo '<a href="'.home_url().'">';
?>
<img id="logo_img" title="<?php bloginfo('name'); ?>" src="<?php echo $logo_path; ?>" alt="<?php bloginfo('name'); ?>">
<strong class="logo">
<?php
if (is_user_logged_in())
{
echo '<a href="http://demo.com/register-path/">';
}
else
{
echo '<a href="'.home_url().'">';
}
echo '<img src="logo-path.png"></a>';
?>
`

Changing PHP request into fixed URL. (Wordpress Template Header)

Very short question but it seems like i'm just to dumb.
<div class="header-content" id="header-content">
<div class="container">
<div id="logo">
<a href="<?php echo home_url(); ?>" title="<?php bloginfo('name'); ?>">
<?php
if(isset($theme_options['logo']) && isset($theme_options['logo']['url']) && $theme_options['logo']['url'] != ''){
echo '<img src="'.$theme_options['logo']['url'].'" alt="" />';
}
else{
echo '<img src="'.get_template_directory_uri().'/assets/img/placeholder/logo.png" alt="" />';
}
?>
</a>
</div>
Since there is only just one "href" in it i thought i could change this line:
<a href="<?php echo home_url(); ?>" title="<?php bloginfo('name'); ?>">
Into:
<a href="http://example.com/" title="<?php bloginfo('name'); ?>">
But after a cache clear and reload nothing changed. In case you didnt figured out what i try, its simple. I want to change the link the blog logo is leading to without changing the blog home url.

Making a header link in a custom Wordpress template

I have a site: www.31publishing.co.uk and want to link the header to the site, but not sure how to do it? The code for header.php is as follows:
Here is the php: http://codepad.org/ek2r2l0J
you can add link in header by replace this code with
<div id="header" style="background: url(<?php echo $header_bg; ?>)">
<div class="inner">
<div id="title_box">
<?php if (get_option('op_logo_on') == 'on') { ?>
<a href="<?php echo home_url() ?>">
<?php $logo = (get_option('op_logo') <> '') ? get_option('op_logo') : get_template_directory_uri() . '/images/logo.png'; ?>
<img src="<?php echo $logo; ?>" alt="Logo" id="logo"/>
</a>
<?php } else { ?>
<a class="site_title" href="<?php echo home_url() ?>/" title="<?php bloginfo('name'); ?>" rel="Home"><h1><?php bloginfo('name'); ?></h1></a>
<?php } ?>
</div>
with below code
<a href="www.softwaretestingawards.com"><div id="header" style="background: url(<?php echo $header_bg; ?>)">
<div class="inner">
<div id="title_box">
<?php if (get_option('op_logo_on') == 'on') { ?>
<a href="<?php echo home_url() ?>">
<?php $logo = (get_option('op_logo') <> '') ? get_option('op_logo') : get_template_directory_uri() . '/images/logo.png'; ?>
<img src="<?php echo $logo; ?>" alt="Logo" id="logo"/>
</a>
<?php } else { ?>
<a class="site_title" href="<?php echo home_url() ?>/" title="<?php bloginfo('name'); ?>" rel="Home"><h1><?php bloginfo('name'); ?></h1></a>
<?php } ?>
</div>
</a>
What do you mean by "...want to link the header to the site"?
The code you posted is the header.php that is included in every page.
It already contains a link that load the home page of your site when you click your logo or site name.
...

Categories