Get post thumbnails in Wordpress - php

This is the code to get the image for post thumbnail, but the image is not found on the page.
Please suggest some ideas.
<?php
add_theme_support( 'post-thumbnails' );
the_post_thumbnail();
set_post_thumbnail_size( 50, 50);
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>

Try this one to get image path and then after pass that path to image scr.
<?php
$image_id = get_post_thumbnail_id($post->ID);
$image_url = wp_get_attachment_image_src($image_id,'large', true); ?>
<a href="<?php the_permalink(); ?>"><img src="<?php echo $image_url[0]; ?>" />
</a>
<?php
}
?>

Try to used
get_the_post_thumbnail( $post_id, 'thumbnail' );
If you want to get thumbnail with custom width and height then used below code
get_the_post_thumbnail( $post_id, array( 100, 100) );
In array first element for width and second for height.

try to pass post id in function
$post_id = get_the_ID();
get_the_post_thumbnail( $post_id, 'thumbnail' );

Try this bro
First Method:
<?php
$imgId = get_post_thumbnail_id($post->ID);
$imgUrl = wp_get_attachment_image_src($imgId,'your image size', true);
?>
<img src="<?php echo $imgUrl[0]; ?>" />
Second Method:
$post_id = get_the_ID();
get_the_post_thumbnail( $post_id, 'your image size' );
Hope you find your solution

Related

How to make img src URL as Featured Image

I am trying to make a "Featured Image" from an img src URL with this theme: https://themezee.com/themes/donovan/
<img src="https://i.imgur.com/UJs4AKj.jpg" />
Please Use below code post Get post featured Image.
<?php
$imageId = get_post_thumbnail_id( $post_id);
$imageURL = wp_get_attachment_image_src( $imageId ), 'full' );
echo '<img src="'.$imageURL.'" />';
I guess this will resolve your problem.
<?php if (has_post_thumbnail( get_the_ID() ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' ); ?>
?>
<img src="<?php echo $image[0]; ?>"/>
<?php endif; ?>

Get featured image alt text wordpress

I'm trying to simple display the alt text for an image that is set in our WP library.
I tried this
<?php $image_id = get_post($id); ?>
<?php $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); ?>
<img class="photo" src="<?php echo the_post_thumbnail_url() ?>" alt="<?php echo $image_alt ?>"/>
But it isn't displaying the alt text when I do that. Anything I'm doing wrong?
If the image is the featured image and you see it correctly, try this:
<?php
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$thumbnail_alt = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
?>
<img class="photo" src="<?php echo the_post_thumbnail_url() ?>" alt="<?php echo $thumbnail_alt ?>"/>
Let me know if you can solve it
THIS WORKED FOR ME
The complete code I used to display the featured image with its alt text
<img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true); ?> "/>
For the image src
<?php echo get_the_post_thumbnail_url(); ?>
For the alt text
<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true); ?>
PS. Make sure you actually give your featured image some alt text in the backend.
Hope it helps someone in the future.

Wordpress featured image as div background image

How can I set the featured images on all my posts to be outputted as a background image to a div. For example
<div class="postimg" style="background-image:url('https://s3.amazonaws.com/ooomf-com-files/8jLdwLg6TLKIQfJcZgDb_Freedom_5.jpg')"></div>
Currently the featured image is being outputted as a regular image using this helper <?php the_post_thumbnail( 'wpbs-featured' ); ?>
I would suggest simply something along the lines of this
Get post featured image URL and echo it out accordingly:
<?php
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "full");
$img = $img[0];
?>
<div class="postimg" style="<?php if($img){echo 'background:url('.$img.');';} ?>">
</div>
Use this
<div style="background-image:url('<?php echo wp_get_attachment_url( get_post_thumbnail_id() );?>')"></div>
Try this...
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>

Woocommerce Grouped Product Images

I'm trying to include a product thumbail alongside the name and price of grouped products. Currently, one parent product includes 20 or so child (grouped) products, and as default the page only displays the quantity chooser, product name, price and add to cart button at the bottom of the list. All of the products are listed in a big table one under the other, and I want to include an individual product image for each seperate product.
Currently, by editing the /single-product/add-to-cart/grouped.php product file, i've managed to get it to display a small image but only of the main parent product by adding the following in a DIV after the product title php:
<div class="images">
<?php if ( has_post_thumbnail( $post_id ) ) { ?>
<a itemprop="image" href="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post_id ) ); ?>" class="zoom" rel="thumbnails" title="<?php echo get_the_title( get_post_thumbnail_id( $post_id ) ); ?>"><?php echo get_the_post_thumbnail( $post_id, apply_filters( 'grouped_product_large_thumbnail_size', 'shop_thumbnail' ), array(
'title' => get_the_title( get_post_thumbnail_id( $post_id ) ),
) ); ?></a>
<?php } ?>
</div>
I know this code is only copied from the bundled product image, but it's the closest i've got. You can see the example of what I mean here:
http://hallmark.digitalstorm.co.uk/product/luxor-custom-built-arrangement/
Any help would be really appreciated, as i know i must be really close.
Here is the code you are looking for:
add_action( 'woocommerce_grouped_product_list_before_price', 'woocommerce_grouped_product_thumbnail' );
function woocommerce_grouped_product_thumbnail( $product ) {
$image_size = array( 20, 20 ); // array( width, height ) image size in pixel
$attachment_id = get_post_meta( $product->id, '_thumbnail_id', true );
?>
<td class="label">
<?php echo wp_get_attachment_image( $attachment_id, $image_size ); ?>
</td>
<?php
}
Hope this will be useful.
Fantastic code by #Ratnakar - Store Apps
For those who try to add the product link to the image
I added a "link" wrap to the image so it will result Image + Link to product
$link = get_the_permalink($product->id);
and
<a href="<?php echo $link; ?>" > <?php echo wp_get_attachment_image( $attachment_id, $image_size ); ?> </a>
Final:
add_action( 'woocommerce_grouped_product_list_before_price', 'woocommerce_grouped_product_thumbnail' );
function woocommerce_grouped_product_thumbnail( $product ) {
$image_size = array( 100, 100 ); // array( width, height ) image size in pixel
$attachment_id = get_post_meta( $product->id, '_thumbnail_id', true );
$link = get_the_permalink($product->id);
?>
<td class="label">
<a href="<?php echo $link; ?>" > <?php echo wp_get_attachment_image( $attachment_id, $image_size ); ?> </a>
</td>
<?php
}
For Woocommerce 2.6 and above (update $product to use data stores):
add_action( 'woocommerce_grouped_product_list_before_price','woocommerce_grouped_product_thumbnail');
function woocommerce_grouped_product_thumbnail( $product ) {
$image_size = array( 100, 100 ); // array( width, height ) image size in pixel
$attachment_id = get_post_meta( $product->get_id(), '_thumbnail_id', true );
$link = get_the_permalink($product->get_id());
?>
<td class="label">
<a href="<?php echo $link; ?>" > <?php echo
wp_get_attachment_image($attachment_id, $image_size ); ?> </a>
</td>
<?php
}

How to get the WordPress post thumbnail (featured image) URL?

I am using this function to get the featured images:
<a href="#" rel="prettyPhoto">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
Now I want to get the full featured image on click on the anchor tag for which I need a featured image URL in
<a href="here" rel="prettyPhoto">
How can I fix this?
Check the code below and let me know if it works for you.
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>
If you want JUST the source, and not an array with other information:
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" />
// Try it inside loop.
<?php
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo $feat_image;
?>
Easy way!
<?php
wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()))
?>
This perfectly worked for me:
<?php echo get_the_post_thumbnail_url($post_id, 'thumbnail'); ?>
I think this is the easiest solution and the updated one:
<?php the_post_thumbnail('single-post-thumbnail'); ?>
This is the simplest answer:
<?php
$img = get_the_post_thumbnail_url($postID, 'post-thumbnail');
?>
You can try this:
<?php
$feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
echo $feat_image;
?>
Try this one
<?php
echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft'));
?>
I had searched a lot and found nothing, until I got this:
<?php echo get_the_post_thumbnail_url( null, 'full' ); ?>
Which simply give you the full image URL without the entire <img> tag.
Hope that can help you.
You can try this.
<?php
$image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<a href="<?php echo $image_url; ?>" rel="prettyPhoto">
You can also get it from post_meta like this:
echo get_post_meta($post->ID, 'featured_image', true);
You can also get the URL for image attachments as follows. It works fine.
if (has_post_thumbnail()) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
}
You will try this
<?php $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'full'); ?> // Here you can manage your image size like medium, thumbnail, or custom size
<img src="<?php echo $url ?>"
/>
<?php
if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
?>
<img src="<?php echo $image[0]; ?>">
<?php endif; ?>
If the post is an image and we already know what the image is, it's possible to get the thumbnail URL without too much hassle:
echo pathinfo($image->guid, PATHINFO_DIRNAME);
Simply inside the loop write <?php the_post_thumbnail_url(); ?> as shown below:-
$args=array('post_type' => 'your_custom_post_type_slug','order' => 'DESC','posts_per_page'=> -1) ;
$the_qyery= new WP_Query($args);
if ($the_qyery->have_posts()) :
while ( $the_qyery->have_posts() ) : $the_qyery->the_post();?>
<div class="col col_4_of_12">
<div class="article_standard_view">
<article class="item">
<div class="item_header">
<img src="<?php the_post_thumbnail_url(); ?>" alt="Post">
</div>
</article>
</div>
</div>
<?php endwhile; endif; ?>
if you want to fetch full image size from post thumbnail you can use this code.
$img_url = wp_get_attachment_image_url(get_post_thumbnail_id(get_the_ID()), 'full');
<img src="<?PHP echo $img_url?> ">
here, get_the_ID() is post id.
Use:
<?php
$image_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail_size');
$feature_image_url = $image_src[0];
?>
You can change the thumbnail_size value as per your required size.
You can also get the URL for image attachments as follows:
<?php
"<div>".wp_get_attachment_url(304, array(50,50), 1)."</div>";
?>
You can get image src for specific size by wp_get_attachment_url function.
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'full' );
?>
<img src="<?php echo $url ?>" />
<img src="<?php echo get_post_meta($post->ID, "mabp_thumbnail_url", true); ?>" alt="<?php the_title(); ?>" width ="100%" height ="" />

Categories