Conditional featured image with youtube thumbnail in Wordpress - php

Basically what I am trying to do here is: Display the youtube thumbnail. If no youtube thumbnail -> Display the Featured image for the post. If no featured image -> Display the fallback image.
However it seems I am doing something wrong because the webpage displays blank.
<?php
// Check if the post has a Youtube thumbnail (using custom field video_url)
if get_post_meta($post->ID,'video_url',true)
echo '<img src="http://img.youtube.com/vi/' . get_post_meta($post->ID,'video_url',true) . '/0.jpg"/>';
// Check if the post has a Post Thumbnail assigned to it
else ( has_post_thumbnail() ) {
echo '<a href="' . get_permalink($post->ID) . '" >';
the_post_thumbnail('frontpage-thumb');
echo '</a>';
// If the post does not have a featured image then display the fallback image
} else {
echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
?>
The general code for displaying the youtube thumbnail with custom fields is <img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/> I just can't get it to work with the conditional statements...

In your first if condition, you have wrong syntax and the PHP must be closed
if (get_post_meta($post->ID,'video_url',true)) //
Correct code
<?php
if (get_post_meta($post->ID,'video_url',true)) ?>
echo '<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/>';
// Check if the post has a Post Thumbnail assigned to it
else ( has_post_thumbnail() ) {
echo '<a href="' . get_permalink($post->ID) . '" >';
the_post_thumbnail('frontpage-thumb');
echo '</a>';
// If the post does not have a featured image then display the fallback image
} else {
echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
?>

Related

Add 'Watermark' to images with php _code

I have a website where users may upload images...
I need to add my logo (watermark) to the images every single time to uploaded.
How can I do so?
Anybody have a good tutorial, article or example for this? Or know of any function in php which I would need to find the position of the watermark?
<?php
if(houzez_edit_property()) {
$property_images = get_post_meta( $property_data->ID, 'fave_property_images', false );
$featured_image_id = get_post_thumbnail_id( $property_data->ID );
$property_images[] = $featured_image_id;
$property_images = array_unique($property_images);
if (!empty($property_images[0])) {
foreach ($property_images as $prop_image_id) {
$is_featured_image = ($featured_image_id == $prop_image_id);
$featured_icon = ($is_featured_image) ? 'text-success' : '';
$img_available = wp_get_attachment_image($prop_image_id, 'thumbnail');
if (!empty($img_available)) {
echo '<div class="col-md-3 col-sm-4 col-6 property-thumb">';
echo wp_get_attachment_image($prop_image_id, 'houzez-item-image-1', false, array('class' => 'img-fluid'));
echo '<div class="upload-gallery-thumb-buttons">';
echo '<button class="icon icon-fav icon-featured" data-property-id="' . intval($property_data->ID) . '" data-attachment-id="' . intval($prop_image_id) . '"><i class="houzez-icon icon-rating-star full-star '.esc_attr($featured_icon).'"></i></button>';
echo '<button class="icon icon-delete" data-property-id="' . intval($property_data->ID) . '" data-attachment-id="' . intval($prop_image_id) . '"><span class="btn-loader houzez-loader-js"></span><i class="houzez-icon icon-remove-circle"></i></button>';
echo '</div>';
echo '<input type="hidden" class="propperty-image-id" name="propperty_image_ids[]" value="' . intval($prop_image_id) . '"/>';
if ($is_featured_image) {
echo '<input type="hidden" class="featured_image_id" name="featured_image_id" value="' . intval($prop_image_id) . '">';
}
echo '</div>';
}
}
}
}
?>
Use PHP GD Library. You can open saved image file, write text or overlay watermark image and save image again.

Product ID should not be accessed directly

I recently updated wordpress and PHP on my testing server. I have fixed a lot of the errors but this one is stumping me. On my shop page, my thumbnails stopped appearing. My products and prices are still showing up correctly. Here is the error message
Notice: id was called incorrectly. Product properties should not be accessed directly.
Here is the PHP
<?php
$placeholder_width = $placeholder_height = 400;
$url = wp_get_attachment_image_src( get_post_thumbnail_id($product->ID), array(260,260) );
if ( has_post_thumbnail() ){?>
<?php if ( is_plugin_active( 'woocommerce-lazyload/woocommerce-lazyload.php' )) {?>
<img data-src="<?php echo get_the_post_thumbnail_url($posts->ID , ['220','220']); ?>" class="attachment-220x220 size-220x220 wp-post-image" width="300" height="300">
<?php }else{ ?>
<img src="<?php echo get_the_post_thumbnail_url($posts->ID , ['220','220']); ?>" class="attachment-220x220 size-220x220 wp-post-image">
<?php }?>
<?php
}
else {
echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="' . $placeholder_width . '" height="' . $placeholder_height . '" />';
}
?>
This chunk is where the problem is. I tried to remove the if statement and jsut have the image and URL grab. I tried to convert $product->ID to $product->get_id() and the latter actually takes the error message away but my thumbnails are not showing up. It must be getting postID correct because the names and prices are showing up and being categorized accordingly. Any suggestions?

default thumbnail for wordpress site

I'm trying to modify my custom wp theme and add related post block. I want to add default thumbnail for posts which doesn't have it. Below code is working fine but i can't archive how to add default img.
$args = array( 'numberposts' => '4','post__not_in' => array($post->ID));
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ) {
if($recent['post_status']=="publish") {
if ( has_post_thumbnail($recent["ID"])) {
echo '<div><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], 'thumbnail'). $recent["post_title"].'</a></div> ';
} else {
echo '<div><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></div>';
}
}
}
In order to print the default thumbnail if the posts featured image is not found you have to print the default image that you have in your images folder.
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-thumb-img.png"
alt="<?php the_title(); ?>" />
<?php } ?>
What the above code does?
It checks whether the post has thumbnails, if not it assigns the default-thumb-img.png ( Change it to your image name) as per your requirement.
my solution is just hardcode absolute link to default thumbnail
$args = array( 'numberposts' => '4','post__not_in' => array($post->ID));
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
if($recent['post_status']=="publish") {
if ( has_post_thumbnail($recent["ID"])) {
echo '<div class="col-md-3 col-lg-3"><div class="recent-post-holder"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], 'thumbnail'). $recent["post_title"].'</a></div></div> ';
} else {
echo '<div class="col-md-3 col-lg-3"><div class="recent-post-holder"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . "<img src='/*add link here*/'>". $recent["post_title"].'</a></div></div>';
}
}
}

Displaying an image via acf in the theme header

I am using AFC Pro and created an options pafe so tjhat the user can upload a new header logo whenever he wants to.
The ACF field is called "headerlogo".
What I want now is that the Logo gets replaced by my theme automatically.
My Variables are:
$headerlogo = wp_get_attachment_image_src(get_field('headerlogo', 'option'), 'full');
$default_logo = '<img src="'echo .$headerlogo[0].'" alt="SITE Logo">';
they get called in:
echo '<a href="'. esc_url( home_url( '/' ) ) .'">
' . $default_logo . '
</a>';
But the Output is:
<a href="http://www.xxx.de/">
<img src="" alt="SITELogo">
</a>
What am I doing wrong here?
Thank in advance.
This should work:
<?php
$headerlogo = get_field('headerlogo');
if( !empty($headerlogo) ):
$default_logo = '<img src="'. $headerlogo['url'] . '" alt="' . $headerlogo['alt'] . '" />';
endif;
echo '' . $default_logo . '';
?>

conditional logic in the header.php

So I am using Wordpress and I have to have a specific logo on a specific page. From research I have to use conditional logic to swap the existing logo with another depending on the current page. Everything I have tried seems to just break the theme.. Any help on guiding me in the correct direction? So basically every page except page_id=79 would have the same logo in the header.
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo'])) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
<?php if ( is_page(79) ) { ?>
What to displayed on page 79.
<?php } else { ?>
What will be displayed everywhere else.
<?php } ?>
This should work.
Try using get_queried_object_id();
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo']) && get_queried_object_id() != 79) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
The url of your logo image is contained within $options['logo']. You should be able to modify this in the admin section of your WordPress installation (try looking in "Appearance -> Header").

Categories