Display featured image URL in the loop - php

I have a problem trying to get the thumbnail URL of each post on the archive page.
I used the basic technics but it always return the url of the first featured image of the page.
Here the part of the code of my template-parts/post/content/content.php
The goal of this is to open the featured image of each post on a lightbox.
Here the link to the page : http://leos-sipek.thomasdesnoyers.com/category/divers-types-dune-ideographie-stochastique/peinture-sur-papier/metaplasme/
If you click on the second post it shows the featured image of the first post.
<div class="post-thumbnail">
<a rel="lightbox" data-gall="gall-frame" data-lightbox-type="inline" href="#inline-content">
<?php the_post_thumbnail( 'twentyseventeen-featured-image' ); ?>
</a>
</div>
<!-- Lightbox -->
<div id="inline-content" style="display:none;">
<?php if (has_post_thumbnail( $post->ID )) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div class="img-single" style="background-image: url('<?php echo $image[0]; ?>')"></div>
</div>
Thanks

Actually I was pointing the same url for each lightbox so it opened the same each time.
I change the Href of each lightbox by the ID of each post and it works.
<div class="post-thumbnail">
<a rel="lightbox" data-gall="gall-frame" data-lightbox-type="inline" href="#<?php the_ID(); ?>">
<?php the_post_thumbnail( 'twentyseventeen-featured-image' ); ?>
</a>
</div>
<!-- Lightbox -->
<div id="<?php the_ID(); ?>" style="display:none;">
<?php if (has_post_thumbnail( $post->ID )) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); endif ?>
<div class="img-single" style="background-image: url('<?php echo $image[0]; ?>')"></div>
</div>
Thanks everyone for your help

In your code you displaying only first element from $image array, try to change this line:
<div class="img-single" style="background-image: url('<?php echo $image[0]; ?>')"></div>
via this code to run through all array elements:
<?php foreach ($image as $image_link) : ?>
<div class="img-single" style="background-image: url('<?php echo $image_link; ?>')"></div>
<?php endforeach; ?>

Why not use the_post_thumbnail_url() ?
<div class="post-thumbnail">
<a rel="lightbox" data-gall="gall-frame" data-lightbox-type="inline" href="#inline-content">
<?php the_post_thumbnail( 'twentyseventeen-featured-image' ); ?>
</a>
</div>
<!-- Lightbox -->
<div id="inline-content" style="display:none;">
<?php if (has_post_thumbnail( $post->ID )) : ?>
<div class="img-single" style="background-image: url('<?php echo get_the_post_thumbnail_url($post->ID, 'single-post-thumbnail'); ?>')"></div>
</div>
However if you are only using one lightbox for all posts in a loop, you will need to set something like a data-url="<?php echo get_the_post_thumbnail_url($post->ID, 'single-post-thumbnail'); ?>" in the lightbox link. Then you can use JS to swap that URL in for the background image on click. Something like:
<script>
(function($) {
$('a[rel="lightbox"]').on('click', function(e) {
var imgSrc = $(this).data('url');
$('#inline-content').css('background-image', imgSrc);
});
})(jQuery);
</script>

Related

How to hide alt tag output on page?

Please check the screenshot, let me know how to fix this issue...And you can see there's no image alt data in HTML.
Another thing is that if I use PHP in img src so I can't set width & height like (300 x 300). The output is a full-size image and if I use a static URL in img it works fine, how to deal with that?
<nav id="nav-single">
<?php
$prev_post = get_previous_post();
$id = $prev_post->ID ;
$permalink = get_permalink( $id );
$thumbnail = get_the_post_thumbnail( $id );
$image_alt = get_post_meta($thumbnail, '_wp_attachment_image_alt', true);
?>
<div class="pexel-previous-post-data">
<div class="pexel-previous-post-img">
<a href="<?php echo $permalink; ?>">
<img src="<?php echo $thumbnail; ?>" alt="<?php echo $image_alt; ?>" width="300" height="300">
</a>
</div>
<div class="pexel-previous-post-Text">
<span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'twentyeleven' ) ); ?>
<h5 class="pxl_head--h5"><?php echo $prev_post->post_title; ?> </h5>
</span>
</div>
</div>
</nav>
Use get_the_post_thumbnail_url() to get post image url as get_the_post_thumbnail() returns html with img tag. get_the_post_thumbnail_url() will return only image url.
$thumbnail = get_the_post_thumbnail_url( $id );

How can I get featured image url for woocommerce Shop page?

This is the code by which I am trying to get featured image url to set as BG. It's working fine for page.php. But in Woocommerce Shop (post-type-archive-product) page it is showing one of product featured image instead of page featured image.
Any solution??
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<header style="background-image: url('<?php echo $thumb['0']; ?>')" class="inner-page-header">
<div class="wrap">
<div class="page_header">
<?php the_title(); ?>
</div>
</div>
</header>
You can use wc_get_page_id() to get the page ID and use that to get the image source. This should work.
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( wc_get_page_id( 'shop' ) ), 'full' );?>
<header style="background-image: url('<?php echo $thumb['0']; ?>')" class="inner-page-header">
<div class="wrap">
<div class="page_header">
<?php the_title(); ?>
</div>
</div>
</header>
Here is the documentation

Wordpress Featured Image Not Displaying

I am trying to show a small version of the featured image as a post thumbnail on my main page (index.php). For this I am implementing it as the background-image of a div. Unfortunately the code (which had been working before) has stopped working now and I cannot find a reason why. I have been looking everywhere for a solution, but I just cant figure it out.
I am using the following code but unfortunately it doesnt return anything:
<?php $post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
<?php if (has_post_thumbnail()) : ?>
<div class="post_image_crop" style="background: url('<?php echo $thumbnail; ?>')">
</div>
<?php endif; ?>
You can have a look at it here: oliverprenzel.com
The weird thing is, I have done exactly the same thing on my single.php where it still does work.
<?php $post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
<?php if (has_post_thumbnail()) : ?>
<div class="post_image_bg" style="background: url('<?php echo $thumbnail; ?>'); background-size: 100% !important;">
</div>
<?php endif; ?>
You can have a look at it working here: oliverprenzel.com/headmagazine/
Does anyone have an idea what might be causing this?
Edit for Claudiu:
This is the loop I am trying to get the image in:
<div class="wrapper">
<?php $post_image_id = get_post_thumbnail_id( $post_id );
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
<!-- post loop prev -->
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>">
<div class="post_frame">
<div class="post_image_crop" style="background: url('<?php echo $thumbnail; ?>')">
</div>
<div class="prev_det">
<div class="prev_det_center">
<h1>
<?php the_title(); ?>
</h1>
<p><?php echo get_the_category_list(', '); ?></p>
</div>
</div>
<div class="prev_det_fold"></div>
</div>
</a>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
If you want to get each posts thumbnail, then you need to move
<?php $post_image_id = get_post_thumbnail_id( $post_id );
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
inside the loop and change it to:
<?php $post_image_id = get_post_thumbnail_id( get_the_ID() );
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
My guess is that $post_image_id is returning null because you don't have a featured image for your home page and even if you had, the same image will be displayed for each post.
I have a strong suspicion that in your first line $post_to_use->ID returns null. When the argument in get_post_thumbnail_id() returns null then the current post id is used by default. This is why it is working in single.php, because there you have a post loaded.
On frontpage you don't. So you have to check which post id you want, manually enter it or do a loop.
Try replacing get_post_thumbnail_id($post_to_use->ID) with get_post_thumbnail_id(5) where 5 is the post id that you want.

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 from External URL not Working Properly

I am trying to use external image as featured image in wordpress. All is done correctly but there is just one problem. The actual external image show only when there is any featured image set via the WP admin. I don't want to set any featured image but still want the featured image from that external URL which is set via a custom field.
I hope you guys got it. Here is the code of my content.php and the external featured image is triggered by get_post_meta($post->ID, "external_featured", true);
<article <?php post_class('single-entry clearfix'); ?>>
<?php if( has_post_thumbnail() ) { ?>
<div class="single-entry-thumbnail view view-first">
<img src="<?php echo get_post_meta($post->ID, "external_featured", true); aq_resize( wp_get_attachment_url( get_post_thumbnail_id(), 'full' ), wpex_img( 'blog_entry_width' ), wpex_img( 'blog_entry_height' ), wpex_img( 'blog_entry_crop' ) ); ?>" alt="<?php echo the_title(); ?>" />
<div class="mask">
<h2><?php the_title(); ?></h2>
<p>
<?php
if( !empty($post->post_excerpt) ) {
the_excerpt();
} else {
echo wp_trim_words(get_the_content(), 20); }
?>
</p>
Download
</div>
</div><!-- /single-entry-thumbnail -->
<?php } ?>
</article><!-- /single-entry -->
Thank you :)
Try this:-
<article <?php post_class('single-entry clearfix'); ?>>
<div class="single-entry-thumbnail view view-first">
<?php $custom_featured_image_url = get_post_meta($post->ID, "external_featured", true); ?>
<img src="<?php echo $custom_featured_image_url; aq_resize( $custom_featured_image_url, wpex_img( 'blog_entry_width' ), wpex_img( 'blog_entry_height' ), wpex_img( 'blog_entry_crop' ) ); ?>" alt="<?php echo the_title(); ?>" />
<div class="mask">
<h2><?php the_title(); ?></h2>
<p>
<?php
if( !empty($post->post_excerpt) ) {
the_excerpt();
} else {
echo wp_trim_words(get_the_content(), 20); }
?>
</p>
Download
</div>
</div><!-- /single-entry-thumbnail -->
</article><!-- /single-entry -->

Categories