Different Logo for Different Pages - php

Trying to show a different logo for page ID 1111 with code snippet below. But it is showing both logos on every page. Not sure what I'm missing or doing wrong. This snippet is in my header.php theme file Wordpress.
<a title="<?php echo esc_attr(get_bloginfo('name')); ?>" href="<?php echo esc_url(home_url()); ?>">
<img src="<?php echo esc_url($log_url); ?>" alt="<?php echo esc_attr(get_bloginfo('name')); ?>" width="<?php echo sh_set( $logo_size, 0); ?>" height="<?php echo sh_set( $logo_size, 1); ?>" >
<?php if (is_page( 1111 )) ?>
<img src="https://teamambrose.realtor/wp-content/uploads/2018/04/favicon.ico.png" alt="<?php echo esc_attr(get_bloginfo('name')); ?>" width="<?php echo sh_set( $logo_size, 0); ?>" height="<?php echo sh_set( $logo_size, 1); ?>" >
</a>
This is what I added to above hoping to only show second image url on page 1111 only.
<?php if (is_page( 1111 )) ?>
<img src="https://teamambrose.realtor/wp-content/uploads/2018/04/favicon.ico.png" alt="<?php echo esc_attr(get_bloginfo('name')); ?>" width="<?php echo sh_set( $logo_size, 0); ?>" height="<?php echo sh_set( $logo_size, 1); ?>" >

The reason why both logos are being shown on every page is because you specified an if-condition but did nothing before you closed the php tag.
Therefore, your extra logo html is being treated as if you added an additional line of HTML code which results in it being shown as any other regular HTML element you have added.
You should add a : behind the if statement to indicate that the line after the if statement is grouped under that particular if statement.
E.g.
<?php if ( is_page ( 1111 ) ) : ?>
<!-- This will only be printed when page id is 1111. -->
<? endif; ?>
You will also have to make sure to end the if statement or all of the code below the if statement will be processed with that if condition.
If what you want to achieve is to show logo A on pages other than 1111 page and only show logo B on page 1111, this is what you should do.
<a title="<?php echo esc_attr(get_bloginfo('name')); ?>" href="<?php echo esc_url(home_url()); ?>">
<?php if ( is_page( 1111 ) ) : ?>
<img src="https://teamambrose.realtor/wp-content/uploads/2018/04/favicon.ico.png" alt="<?php echo esc_attr(get_bloginfo('name')); ?>" width="<?php echo sh_set( $logo_size, 0); ?>" height="<?php echo sh_set( $logo_size, 1); ?>" >
<?php else : ?>
<img src="<?php echo esc_url($log_url); ?>" alt="<?php echo esc_attr(get_bloginfo('name')); ?>" width="<?php echo sh_set( $logo_size, 0); ?>" height="<?php echo sh_set( $logo_size, 1); ?>" >
<?php endif; ?>
</a>

Related

put default image if there is no image using ACF

I am using Wordpress ACF plugin and I have custom products section and I put a custom field for featured image and it's working perfect.
Now I want if there is no featured image inserted then show a default image, I am using the following code but its not working.
<? php
$category_image = get_field('fimage');
if ($category_image) {
$category_image_url = $category_image['sizes']['product-thumb'];
}
if ($category_image_url) { ?>
<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/timthumb.php?src=<?php echo $category_image['url']; ?>&w=300&h=250&zc=0" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
} else($category_image_url) { ?>
<img src="http://www.staticwhich.co.uk/static/images/products/no-image/no-image-available.png" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
<? php } ?>
$category_image="";
$category_image_url="";
$category_image = get_field('fimage');
if($category_image){
$category_image_url = $category_image['sizes']['product-thumb'];
}
if($category_image_url) { ?>
<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/timthumb.php?src=<?php echo $category_image['url']; ?>&w=300&h=250&zc=0" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
<?php
}
else { ?>
<img src="http://www.staticwhich.co.uk/static/images/products/no-image/no-image-available.png" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
<?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 } ?>

Wordpress - Custom Header

Im trying to get a custom header to my wordpress theme.
$args = array(
'width' => 1920,
'height' => 800,
'default-image' => get_template_directory_uri() . '/images/header.jpg',
'uploads' => true,
); add_theme_support( 'custom-header', $args );
Added that to my functions.php and want the default-image to be the image that show if its no image set in admin.
But it wont show anything. the "img src" is empty.
Tried to upload a image then it shows, what could be the problem?
<img src="<?php echo header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />
Using that in my front-page.php (the width and height is set correctly).
I couldn't find anything wrong with your code. However you can try this below. It always works well for me.
<?php if (get_header_image()) { ?>
<img src="<?php header_image(); ?>" alt="<?php bloginfo('name'); ?>">
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/header.jpg" alt="<?php bloginfo('name'); ?>">
<?php } ?>
Try:
<img src="<?php echo( get_header_image() ); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />

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>
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');
}

PHP Product Url

I have the below code in a page.
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<div class="listing-type-list catalog-listing top10full" onclick='window.open("http://google.com")'>
<?php
$i = 0;
foreach ($_productCollection as $_product):
$i++;
?>
<div class="listing-number"><p class="listing-position"><?php echo $i ?></p></div>
<div class="listing-item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
<?php // Product Image ?>
<div class="product-image">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getSmallImageLabel()) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</div>
I would like to replace http://google.com with the product url:
<?php echo $_product->getProductUrl() ?>
However the above way of getting the product url only works when inside the foreach loop.
How can I get this to work?
Which product do you want to use to replace http://google.com? There is one link and many products.
You can try to use first one <?php $pr = $_productCollection[0]; echo $pr->getProductUrl() ?>

Categories