I can't retrieve featured image url through wordpress - php

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

Related

Get the URL of the first image from the product gallery in Woocommerce

How can I get the URL of the first image that is uploaded to the product gallery?
I am not talking about the featured image.
I would appreciate any suggestions.
The following will give you the first image Url from the product gallery:
global $post, $product;
if( is_a($product, 'WC_Product'))
$product = wc_get_product($post->ID);
$product = wc_get_product(40);
// Get the array of the gallery attachement IDs
$attachment_ids = $product->get_gallery_image_ids();
if( sizeof($attachment_ids) > 0 ){
$first_attachment_id = reset($attachment_ids);
$link = wp_get_attachment_image_src( $first_attachment_id, 'full' )[0];
// Output
echo $link;
}

Set featured image via post content programmatically

I was trying to add featured image from post content. My idea is to set the first image of the content as featured image of the post.
I wrote this in my funtions.php
add_action('publish_post', 'auto_featured_image_publish_post');
function auto_featured_image_publish_post($post, $post_id) {
$thumnail_id = //something I need help with
set_post_thumbnail( $post, $thumbnail_id );
}
This is just an idea how I want to do this.
Please help. Thanks
Check this url
https://www.gavick.com/blog/wordpress-quick-tip-4-automatically-set-the-first-post-image-as-a-featured-image
OR you can use this code to get the url of the first image from any HTML code
<?php
$postcontent = "";
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $postcontent, $image);
// you can use the exist pattern or use this
// '/< *img[^>]*src *= *["\']?([^"\']*)/i'
echo $image['src'];
?>

I am trying to use ACF repeater to call a random image on a wordpress site

This is my code, as aforesaid - i'm trying to get a single picture to echo inside a premade div out of repeater element. Despite working directly with the example on the website - I would love some help.
<?php
$repeater = get_field('left_column_repeater' ); // get all the rows
$rand_row = $repeater[ array_rand( $repeater ) ]; // get a random row
$rand_row_image = $rand_row['left_column_image' ]; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2]; ?>
<div class="circular" style="background-image: url('<?php echo $image[0]; ?>');"> </div>
Where did I go wrong?

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

how to get post thumbnail using post id in wordpress?

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

Categories