Wordpress Featured Image URL - php

I am trying to add the featured image url to the featured image, so it will open up in a lightbox when clicked.
My code is:
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
</article><!--/ Article -->
<?php endif; ?>
I have tried quite a few things but cannot get it to pull the thumbnail URL. I have tried adding these to where the link # is:
wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ));
...
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
...
<?php echo $thumbnailsrc ?>
I am sure there is a simple solution that I am yet to find. Thanks to anyone who maybe able to assist :-)

please try this code adding in to your post loop. and confirm you have set feature image for that post which you listings.
<?php $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
if(!empty($post_thumbnail_id)) {
$img_ar = wp_get_attachment_image_src( $post_thumbnail_id, 'full' ); ?>
<img src="<?php echo $img_ar[0];?>" />
<?php } ?>

Try this:
wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' )[0]
Assuming you are in The Loop, you can simply use get_post_thumbnail_id() to get the current post featured image ID. You also need to pass the second parameter full in order to get the non-resized version of the image for your lightbox source. You can limit it to large as well, if you like.

Related

Wordpress - How to make thumbnail a link to post?

I have some problem. I use Wordpress theme - Intergalatic. But in homepage, the large background of post do not take us to the post. The background must be clickable and move us to the post. How to do it?
This is code of thumbnail.
<?php if ( has_post_thumbnail() ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'intergalactic-large' ); ?>
<div class="entry-background" style="background-image:url(<?php echo esc_url( $thumbnail[0] ); ?>)"></div>
<?php } ?>
Assuming you're in the loop, and by the looks of it you are, then you'll need to change div to anchor
<?php if ( has_post_thumbnail() ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'intergalactic-large' ); ?>
<?php } ?>
And probably change the anchor to display:inline-background; and possibly add the height of the image to the styling like:
<?php if ( has_post_thumbnail() ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'intergalactic-large' ); ?>
<?php } ?>

Wordpress showing a default image on single.php

I was hoping someone might be able to help with an issue I'm having...
I'm currently building a Wordpress site and I'm putting together the posts section. Everything seems ok, when using a featured image on a post, it displays nicely in a thumbnail here: http://5.10.105.45/~learningforsusta/index.php/education-for-sustainable-development/ and then nicely inside the single post here: http://5.10.105.45/~learningforsusta/index.php/efsc-post/example-post-1/
However, on a post which doesn't have a featured image specified, it seems to be displaying a default image...
My question is, how can I get rid of the default image, if there is no featured image, I would like it to not display anything...
Here's the code I'm using to pull it all through:
<div class="container">
<div class="row">
<div class="col-md-9">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="page-header">
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
$thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);
?>
<p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>
<p><em>
By <?php the_author(); ?>
on <?php echo the_time('l, F jS, Y');?>
in <?php the_category( ', ' ); ?>.
<?php comments_number(); ?>
</em></p>
</div>
<?php the_content(); ?>
<hr>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<div class="page-header">
<h1>Oh no!</h1>
</div>
<p>No content is appearing for this page!</p>
<?php endif; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
and here's the code from the functions.php file that relates to the thumbnails...
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size(150, 120, true);
I'm sure I'm missing something simple, but I've tried altering things and i just can't figure it out!
Any help would be greatly appreciated...
Thanks,
Shaun
Make sure that you don't have any function attached to post_thumbnail_html filter, take a look at your functions.php (the best option would be to search in a whole wp-content directory, apply *.php filter to reduce result set)
Make sure you don't have any third-party plugin which can do this behind the scenes, e.g. this one.
You can try by using has_post_thumbnail() function and display with the_post_thumbnail()
<?php if ( has_post_thumbnail()): // Check if thumbnail exists ?>
<p class="featured-image">
<?php the_post_thumbnail('post-thumbnails'); ?>
</p>
<?php endif; ?>
Just check for existence of featured image before outputting it:
<?php
if( has_post_thumbnail() ):
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
$thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);?>
<p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>
<?php endif;?>

Get links and featured image from a specific category

I am trying to create a custom slider in wordpress and add my own links but I am not able to get the featured image of a post to display. Here is my code that I am working with right now.
<?php $posts = get_posts('category=20&orderby=rand&numberposts=6'); foreach($posts as $post) { ?>
<?php echo '<li>'; ?>
<a href="<?php the_permalink() ?>" target="_parent"><?php the_title();?>
<?php wp_get_attachment_image( $attachment->ID, 'small' );?>
</a>
<?php echo '</li>'; ?>
This code works by displaying the links randomly but they never display the featured image of a post. Can someone please help out and let me know what i am doing wrong here...
Thanks
It is because the $attachment variable is null. I'd suggest to replace:
<?php wp_get_attachment_image( $attachment->ID, 'small' );?>
with the following instead:
<?php the_post_thumbnail( 'small' );?>

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

Featured image for custom post type

I am working on the home page of this site.
I have successfully placed a featured image thumbnail of the latest blog post in the 3rd column on the right. I am not trying to make the first column featured image thumbnail come from a custom post type I've created called portfolio_item
I've used the following code to place the blog post featured image:
<?php query_posts('showposts=1'); ?>
<?php while (have_posts()): the_post(); ?>
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); ?>
<img class="aligncenter circle" alt="" src="<?php echo $image[0]; ?>" />
<?php endif; ?>
<?php endwhile;
wp_reset_query();
?>
What do I need to change to make it retrieve the featured image for the latest post in the custom post type portfolio_item?
query_posts(array ( 'showposts' => 1 , 'post_type' => 'portfolio_item') );

Categories