put default image if there is no image using ACF - php

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 } ?>

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 } ?>

Why My Conditional Tag Didn't work in Wordpress

In My Wordpress Blog BuildinBit. I Try To Assing H1 to post title rather than blog title on single posts and single pages.
And on the Home page, Archive page, Category and Search Page.I Try To Assing H1 To My Blog Title.
For This I Use Following Code:
<?php
if(get_header_image()) {
?>
<img id="logo" src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="<?php echo bloginfo('name'); ?>" title="<?php _e( 'back to home', 'blogkori' );?>"/>
<p id="tagline"><?php echo bloginfo('description'); ?></p>
<?php
} else {
?>
<?php if(is_home() || is_category()|| is_tag() ||is_archive() ||is_search() ){
<h1 id="title"><?php echo bloginfo('name'); ?></h1>
}else
{
<span id="title"><?php echo bloginfo('name'); ?></span>
}
?>
<?php
}
?>
But This Code Didn't work it show HTTP Error 500 on all pages.
// Close php tag like this
<?php if(get_header_image()) { ?>
<img id="logo" src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="<?php echo bloginfo('name'); ?>" title="<?php _e( 'back to home', 'blogkori' );?>"/>
<p id="tagline"><?php echo bloginfo('description'); ?></p>
<?php } else { ?>
<?php if(is_home() || is_category()|| is_tag() ||is_archive() ||is_search() ){ ?>
<h1 id="title"><?php echo bloginfo('name'); ?></h1>
<?php }else { ?>
<span id="title"><?php echo bloginfo('name'); ?></span>
<?php
}
} ?>

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 } ?>

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>';
?>
`

Categories