Wordpress - Custom Header - php

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="" />

Related

Different Logo for Different Pages

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>

Put default image in ACF

I want to put a default image in custom taxonomy category using Advanced Custom Field Pro plugin.
I am using following code to show the featured image, I want to show a default image if there is no featured image.
<?php
if($category_image_url) { ?>
<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/timthumb.php?src=<?php echo $category_image['url']; ?>&w=250&h=200&zc=2"
alt="<?php echo $term->name; ?>"
class="product_img">
<?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 } ?>

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 : ACF image not showinhttp://stackexchange.com/g correctly

I have an issue trying to display an image. I use last ACF
but not showing image
when i take look in html i get this :
<img src="2119, , , , , image/jpeg, /wp-content/uploads/2016/02/ef51d8e8f5cf684e7654f0ea4ab7ce33.jpg, 1024, 681, Array" alt="">
my name slug is hotes
<span><img src="<?php the_field('hotes',256); ?>" alt="" /><span>
the post meta for image is an array of data containing url, height, width etc.
based on the docs,
http://www.advancedcustomfields.com/resources/code-examples/
http://www.advancedcustomfields.com/resources/image/
you should be doing it like this,
<?php $image = wp_get_attachment_image_src(get_field('image_test'), 'full'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('image_test')) ?>" />
or this way,
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
or by using native get_post_meta function
$image = get_post_meta(get_the_ID(), 'hotes'); // Get Image Object
$image_url = $image['url']; // Retrieve image url

Categories