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 } ?>
Related
I am trying to make a "Featured Image" from an img src URL with this theme: https://themezee.com/themes/donovan/
<img src="https://i.imgur.com/UJs4AKj.jpg" />
Please Use below code post Get post featured Image.
<?php
$imageId = get_post_thumbnail_id( $post_id);
$imageURL = wp_get_attachment_image_src( $imageId ), 'full' );
echo '<img src="'.$imageURL.'" />';
I guess this will resolve your problem.
<?php if (has_post_thumbnail( get_the_ID() ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' ); ?>
?>
<img src="<?php echo $image[0]; ?>"/>
<?php endif; ?>
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.
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; ?>
I found a simple code that gets the featured image from a post.
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 1680,470 ), false, '' );
echo $src[0];
?>
I need this for a page that uses a image from a category "slider" and sets featured image. This will make a header image on the page.
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 1680,470 ), false, '' );
?>
<div id="header-hero" style="background-image: url('<?php echo $src[0]; ?>');">
But if someone makes a new post in another category it fails. So, how can I get the image from the category? It will be only one image in the category so it makes it a little easier. Hope for some wordpress-gurus :)
Use WP_query, with parameters in array.
$args = array(
'category_name'=>'your-category-slug',
'posts_per_page'=> 10,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
//Post data
echo get_the_post_thumbnail(get_the_ID());
endwhile;
Try this:
<?php
$slider_category_id = 123213;
query_posts('showposts=1&cat='.$slider_category_id);
if (have_posts()) : while (have_posts()) : the_post();
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 1680,470 ), false, '' );
?>
<div id="header-hero" style="background-image: url('<?php echo $src[0]; ?>');">
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
i'm trying to get a thumbnail to display if it exists within a div class, but it's outputting code in unexpected ways (like the permalink is outside of the href)
what am i doing wrong?
<?php
if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo '<div class="thumbnail">' . $image[0] . '</div>';
} else {
echo '';
}
?>
results in
http://www.permalink.com/<div class="thumbnail">http://www.mysite.com/wp_myblog/wp-content/uploads/2011/10/fretless-thumbnail1.jpg</div>
and no, i didn't leave out any carrots, brackets, quotes or any other code. this is copy and past exactly how it is outputing
EDIT : THE FIX
i had to add some extra html since the fix only spit out the jpg url sans img tags. plus, it wasn't displaying the correct image - it was showing the original jpg instead of the thumbnail version
<?php
if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
echo '<div class="thumbnail">' . '<img src="' . $image[0] . '"></div>';
}
?>
YAY!
The the_permalink function includes an echo statement already.
Change it to get_permalink, and it should work correctly:
<?php
if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo '<div class="thumbnail">' . $image[0] . '</div>';
}
?>
You don't really need the else bit, either. It's probably redundant.
In fact, for a slightly more neater alternative, this would probably work (amended from my own code; just added the link):
<?php if ( has_post_thumbnail() ) : ?>
<div class="hover_img">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
</div>
<?php endif; ?>