how to get post thumbnail using post id in wordpress? - php

I am trying to get the post thumbnail using post_id,but i am getting so many problems.
Iam calling the function in a separate php file in theme directory
echo get_the_post_thumbnail('637');
Fatal error: Call to undefined function get_the_post_thumbnail() in ...
1)can we get the thumbnail using post_id
or
2)can we get the image source using post_id
please any body help me
Thanks in advance

Try this
global $post;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post');
echo $thumb[0];

In your case you make a small mistake that you put the single quote inside the function when function require an integer value.
echo get_the_post_thumbnail('637');
Bellow code are valid try it.
Simple Form
echo get_the_post_thumbnail(637);
Size Specified Form where second argument is the size of the image.
echo get_the_post_thumbnail(637, array(100,100));
also you can try bellow code also
get_the_post_thumbnail(637); // without parameter -> Thumbnail
get_the_post_thumbnail(637, 'thumbnail'); // Thumbnail
get_the_post_thumbnail(637, 'medium'); // Medium resolution
get_the_post_thumbnail(637, 'large'); // Large resolution
get_the_post_thumbnail(637, 'full'); // Original resolution
Also you can refer to the WordPress codex Here.
I am also going to write a full post on this topic on my blog

Use Require_once Or include_once
require_once('/the/path/to/your/wp-blog-header.php');
include_once('wp-blog-header.php' );
get_the_post_thumbnail($post_id); // without parameter -> Thumbnail
get_the_post_thumbnail($post_id, 'thumbnail'); // Thumbnail
get_the_post_thumbnail($post_id, 'medium'); // Medium resolution
get_the_post_thumbnail($post_id, 'large'); // Large resolution
get_the_post_thumbnail($post_id, 'full'); // Original resolution
get_the_post_thumbnail($post_id, array(100,100) ); // Other resolutions
Out side of loop
global $post;
if (has_post_thumbnail( $post->ID ) ){
//
get_the_post_thumbnail($post->ID);
//
}

Vallabh's solution works. This is how I use it as a background image:
<?php if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(637), 'thumbnail' );
$image = $image[0];
} ?>
<div style="background-image: url(<?php echo $image; ?>)"> ... </div>

Create a post template..look like this(post_temp.php)
<?php
$args=array('order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
$query=new WP_Query($args);
if( $query->have_posts()):
while( $query->have_posts()): $query->the_post();
{
echo get_the_post_thumbnail($post->ID);
}
endwhile;
else:
endif;
?>

Related

Get the post thumbnail URL and assign to variable

I'm having a very strange issue. All I want to do is get the thumbnail url and assign it to a variable. Here is my code.
<?php /* Template for displaying content of MH Posts Large widget */ ?>
<article class="post-<?php the_ID(); ?> mh-posts-large-item">
<figure class="mh-posts-large-thumb">
<?php
$form_image = 'blank';
if (has_post_thumbnail()) {
$form_image = the_post_thumbnail_url('mh-magazine-lite-content');
?>
Basically, if the post has a thumbnail I want to store the actual URL of the thumbnail used in that variable for later use. However, instead of doing that it just prints the URL on screen and doesn't actually seem to put it in the variable.
I don't understand why and I would definitely appreciate any help! :)
Looking at the official documentation:
function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
$url = get_the_post_thumbnail_url( null, $size );
if ( $url ) {
echo esc_url( $url );
}
}
So the_post_thumbnail_url only outputs the URL it gets from get_the_post_thumbnail_url and doesn't return anything. Thus the solution is to use get_the_post_thumbnail_url directly.

Is there a Wordpress php function that can return just the URL of the Custom Logo image(not the full image tag)?

I am trying to code a small custom wordpress theme. So I am overriding on the twentytwenty theme of wordpress.
Just to be clear I am talking PHP and overriding home.php, where I want to create some custom html header.
I am trying to get the url of the logo (that I (or the user, can change) using the wordpress customizer.
What I did is :
<img src="<?php echo (get_custom_logo()) ? get_custom_logo() : 'somefallback_url'; ?>" >
What is happening is :
get_custom_logo() function is returning an Image which is normal, I can't find the function that should return the URL in the codex. An error is happening i have an image inside another one.
So basically what I want is :
A PHP function that returns just the URL not the full Image tag.
For a short one-liner:
<?php echo esc_url( wp_get_attachment_image_src( get_theme_mod( 'custom_logo' ), 'full' )[0] ); ?>
The codex lies it out for you
function get_custom_logo_url()
{
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
return $image[0];
}
if you want to return only the image src URL you can use this with the simplest way.
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image_url = wp_get_attachment_image_src ( $custom_logo_id , 'full' );

I can't retrieve featured image url through wordpress

So I am trying to call an image to my main page (Home page). This image lives on a post and it is set as a featured image on that said post. So I got the post ID and I was able to display the title and content on the front page. But the featured image won't display the url. So here is the code I have on my front page:
<?php
$post_id = 53;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post_id));
echo $title;
echo $image;
echo $queried_post->post_content;
?>
It just outputs array. Thank you for the help.
Here is the solution for your problem.
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$thumb_images = wp_get_attachment_url($post_thumbnail_id);
echo $thumb_images;
//Here you will get url of featured image.
This following code can help you with your problem.
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'single-post-thumbnail' ); // get array of featured image
echo $image[0]; //get url of featured image of post

How to get featured image thumbnail instead of cropping automatically?

I'm working on a gallery for the homepage of this site: Warm Glow Photo
I have called the featured image at a specific size (800x600) that I have defined in functions.php. This seems to be calling
...IMAGE.jpg?resize=800%2C600
instead of the version of the featured image called
...IMAGE-800x600.jpg
I am using a plug-in for cropping thumbnails which means I need to call this image rather than cropping with ?resize.
I've found lots of info about how to call different size thumbnails but nothing that explains why it crops with ?resize instead of calling the different thumbnail itself. Any ideas on how to do this would be much appreciated.
The relevant code is:
<?php
if ( has_post_thumbnail() ) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
echo '<li><a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">';
the_post_thumbnail( 'bones-thumb-800' );
echo '</a></li>';
}
?>
and defining bones-thumb-800 in functions.php
add_image_size( 'bones-thumb-800', 800, 600, true );
To get the plain URL of a post use this function :
function um_get_post_featured_image_src($post_id = null,$size = "full"){
if(!$post_id){
global $post;
$post_id = $post->ID;
}
if(has_post_thumbnail($post_id)){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
return $image[0];
}else{
return "";
}
}
If you have a $post globally than you don't need to pass post ID.

wordpress different banner images for each pages - dynamically

My website consist of 5 inner pages, and I want to use different banner images for each page. The page is with side bar, but I want a full width banner, so I used a code which I got from wordpress and its working
this is the code..
<div class="banner">
<?php
if( is_page('About') ) $img = 'bannerAbout.jpg';
elseif( is_page('Services') ) $img = 'bannerServices.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_home() ) $img = 'bannerBlog.jpg';
else $img = 'banner.jpg';?>
<img alt="" src="<?php bloginfo('stylesheet_directory'); ?>/images/<?php echo $img;?>" />
</div>
My question is, How Can I call 'Featured Image' of each page in this code? between $img=""
Or any plug-for this?
If I can call the featured image, then its easy to upload images, otherwise I need to use FTP all the time to change.
Please help me.
Thanks in Advance
Add this code to your theme's functions.php
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
this will enable featured image for post and page
Then in your page.php file add this code
before content and sidebar
<?php
global $post;
echo get_the_post_thumbnail($post->ID, 'full'); // visit http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail for more info
?>
Place below code in each condition
global $post
echo get_the_post_thumbnail( $post->ID,'full' );
full will take the full size of image what ever you have uploaded

Categories