How to assign links to thumbnail images. I have portfolio with thumbnail images of different websites and i have to add link to them.
<div class="showcase-image">
<?php
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
if(has_post_thumbnail()): echo '<img src="'.$thumbnail[0].'"/>'; else: ?>
<?php endif; ?>
</div>
Try this
<div class="showcase-image">
<?php
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
if(has_post_thumbnail()): echo '<a href="'.$thumbnail[0].'" > <img src="'.$thumbnail[0].'"/></a>'; else: ?>
<?php endif; ?>
</div>
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 have a loop that gets the title, content, and link of all pages that are a child of the current page. Everything is working except I don't know how to get the featured image url for these pages so that I can use it as a background image. You can see in the code where I have commented where I am trying to get the url of the featured image.
Here is my code:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'asc' ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
$trimmed_content = wp_trim_words( $content, 14, "..." );
?>
<div class="service-box block-grid-item">
<div class="row">
<div class="col-xs-6 col service-image">
<!-- This is where I am trying to get the featured image url -->
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<a href="<?php echo get_page_link( $page->ID ); ?>" style="background-image: url(<?php echo '$feat_image'; ?>)"</a>
</div>
<div class="col-xs-6 col service-body">
<a href="<?php echo get_page_link( $page->ID ); ?>">
<span class="service-title"><?php echo $page->post_title; ?></span>
<span class="service-text"><?php echo $trimmed_content; ?></span>
<span class="service-link">Read more</span>
</a>
</div>
</div>
</div>
<?php }
?>
You're close, but you should be using wp_get_attachment_image_src(), and should remove the quotes from your variable when you echo the source:
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
<a href="<?php echo get_permalink( $page->ID ); ?>"
style="background-image: url(<?php echo $feat_image; ?>)">
You should also take a look at your HTML markup and make sure it's valid.
on above answer some part of code is missing. here is the complete code.
<?php $feat_image = wp_get_attachment_image_src( get_post_thumbnail_id( 5 ), 'large' );
print_r($feat_image);
?>
<a href="<?php echo get_permalink( '5' ); ?>"
style="background-image: url(<?php echo $feat_image[0]; ?>)"> </a>
I have a website that i'm working on where I need to generate a unique image in header.php for each individual post.
I have this in my index.php:
$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach($pageposts as $ppost){
$ID = $ppost->post_id;
$title = get_the_title( $ID );
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'single_post_thumbnail' );
$t = $thumb[0];
$desc = get_post($ID)->post_content;
$out = get_post_meta($ID,'outbound',true);
$output = "
<div class='ph-sticky' id='phf'>
<span class='icon-x'><i class='fa fa-times icon-xy'></i></span>
<div class='row hunt-row-fp'>
<a class='title' href='$out' target='_blank' rel='nofollow'>$title</a>
<div class='img-featured'><img class='phsi' src='$t'/></div>
<span class='description'>$desc</span>
</div>
</div>";
}
echo $output;
wp_reset_query();
?>
This correctly generates a thumbnail for each post, however in my header.php my code only generates the image of the FIRST post on the page. Here it is:
<!-- post modal -->
<div class="show-post-modal">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?>
<div class="comments-bg-image" style="background-image: url('<?php echo $image[0]; ?>')" >
</div>
<?php endif; ?>
<div class="comments-header">
I've tried multiple solutions however I can only seem to pull in the first thumbnail, which becomes the background for all posts. Any help would be most appreciated!
i think this will help you a lot.
<?php
$page = get_post($post->ID);
$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' );?>
<style>
.comments-bg-image{ background-image: url(<?php echo $img_ar[0];?>); }
</style>
<?php } ?>
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; ?>
this is in my header
<meta property="og:image" content="<?php echo get_the_post_thumbnail($post->ID, 'thumbnail'); ?>" />
this is what is on my page so this where the featured image is taken from
while (have_posts()) : the_post();
$image_share = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-share' );
$image_post = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'counsel' );
$image_full = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$comments_count = wp_count_comments( $post->ID );?>
<h3 class="post-info-title"><p class="poster-title"><?php the_title();?></p>
<p class="post-comment"><span class="post-comment-text">Comments</span><span class="post-commment-num"><?php echo $comments_count->approved ;?></span><img class="img-comment" src="<?php echo 'http://edge.beresponsive.net/wp-content/themes/edge-church/images/comment-img.png';?>" /></p>
<p class="share-link-holder share-news"><a class="share-links" onclick="window.open('//player.vimeo.com/v2/video/<?php the_field('vimeo_link');?>/share/facebook', 'Facebook Share', 'width=400, height=300'); return false;" href="javascript:void(0)">Share This</a></p></h3>
<div class="news_holder">
<a class="news_img" href="<?php the_permalink();?>">
</a>
<div class="blur post-cont">
<div class="blurb-text"> <?php the_content(); ?></div>
</div>
</div>
<?php endwhile; ?>>
Use get queried object
$queried_object = get_queried_object();
echo get_the_post_thumbnail($queried_object->ID, 'thumbnail');
see ref. on wp codex
Print the $image into your content="";
$page_id = get_queried_object_id();
if ( has_post_thumbnail( $page_id ) ) :
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $page_id ), 'optional-size' );
$image = $image_array[0];
else :
$image = get_template_directory_uri() . '/images/default-bg.jpg';
endif;
<meta property="og:image" content="<?php echo $image; ?>" />