This article is about my experience with the “wordpress php get featured image url”.once, I wanted to get a featured image URL, and my website theme didn’t support this option. If you have experienced the same problem or you just want to access the featured image URL in wordpress, stay with me! You can add this option to the wordpress theme with a simple little code.
wordpress php get featured image in 5 minutes!
To get featured image url, follow the instructions:
- Open your wordpress theme folder and find the “functions.php” file.
- Add this code to “functions.php”.
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
This code lets you use the featured image on your website.
If you want to show a wordpress featured image on the website homepage, simply use this code:
<?php
the_post_thumbnail();
?>
Note: when you don’t mention size in Parentheses default size “thumbnail” will be set automatically.
If you wish to set the size as you desire, here is the five standard size that you can use:
the_post_thumbnail( 'thumbnail' );
the_post_thumbnail( 'medium' );
the_post_thumbnail( 'medium_large' );
the_post_thumbnail( 'large' );
the_post_thumbnail( 'full' );
Note: use this code only in the content display loop. As a sample:
<?php while ( have_posts() ) : the_post();?>
<?php the_post_thumbnail(); ?>
<?php endwhile;?>
wordpress php get featured image url
I already explained how to define featured image for the wordpress theme, but what if you want to get image URL to use in different parts of the website? Imagine you want to use a featured image as a background for a section. Attention! This part of the code is not handy at all!
<img src="<?php the_post_thumbnail(); ?>">
But why is it wrong to use this code? As you see, you have put an image on the “img tag,” not its address! In fact, we need to put the image address URL in the “image tag,” not the image itself!
So we want to solve the problem: wordpress php get featured image url. Simply use this code:
<?php
$featured_img_url = get_the_post_thumbnail_url(get_the_ID());
?>
Instead of “featured_img_url,” you can use any name to set a variable. By using that code, I mentioned you could save the wordpress featured image address in the “featured_img_url” variable.
Now, just use it anywhere you want.
Here is the right code:
<img src="<?php echo $featured_img_url; ?>">
As you see, I’ve put the amount in the varible “featured_img_url” in “image tag”!
What is it? YES! wordpress featured image url!
The job is done! As you see, we can easily get the wordpress php featured image url by using some simple code. I hope this tutorial in wpexpelor is useful for you, and as usual, ask me any questions in the comments. It would be great to hear from you! Good Luke!