echo mix of php and html - synatx - php

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

Related

How to make img src URL as Featured Image

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

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

Linking Post URL to Thumbnails on Slider

The below code pulls in all the thumbnails of posts in a category when you are viewing one of the posts in that category. I need to link the title of the post to the image.
<div id="workSlider" class="flexslider">
<ul class="slides">
<?php while (have_posts()) : the_post(); ?>
<?php $workthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'full' );
if( $workthumb && $currentID != $post->ID ) {
echo '<li><img src="'. $workthumb[0].'" ></li>';
}
?>
<?php endwhile; ?>
</ul>
</div>
Try echo '<li><img src="'. get_the_title() .'" ></li>'; I think this will work.

Wordpress Featured Image URL

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.

WORDPRESS Post Thumbnail -> Link to post

I've made a custom metabox that if selected makes the post thumbnail the background of the site.
Now I need that this post thumbnail has a link to the post.
< ?php query_posts ('showposts=5$cat=2');
if (have_posts()) : ?>
if ( has_post_thumbnail() && get_post_meta($post->ID, 'dbt_checkbox', true) ) {
the_post_thumbnail('background');
}
else {}
?>
<?php endwhile; endif; ?>
Wordpress documentation provides an example for this exact situation
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
However, I kind of hate opening and closing php tags so lightly. I'll try something to improve this answer.
Edit: got it. Please try this:
if ( has_post_thumbnail() && get_post_meta($post->ID, 'dbt_checkbox', true) ) {
echo '<a href="' . get_permalink( $post->ID ) . '" >';
echo get_the_post_thumbnail( $post->ID, 'background' );
echo '</a>';
}

Categories