WordPress get post thumbnail and wrap it in permalink - php

I would like to get the post thumbnail and link it to the post in WordPress. This seems logical to me but it's not producing the desired result.
<?php
if(is_category()){
echo '' . '<img src="' . the_post_thumbnail( 'full' ) . '"';
}
?>
Instead, it's producing this in the DOM:
What do I need to do to get this working?

This worked for me:
echo '<img src="' . get_the_post_thumbnail() . '';

Have you tried the 'get_the_post_thumbnail_url()' function? As described on this page https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
Also I'm not sure if the 'full' size is correct, maybe first try it without the optional size parameter. This only works while "in the loop", or you'll have to specify the post ID as the first parameter.
You might want to create an if statement to display the post tile if there isn't a post thumbnail (the function returns false if this is the case).
<?php
if(is_category()){
echo '' . '<img src="' . get_the_post_thumbnail_url( 'full' ) . '"/>';
}
?>

Related

If the image field is empty how do I get it to show nothing

I have the following image field code but if there's nothing there then the alt stage and src= parts still show, how can I get rid of them if the field is empty?
echo '<div class="tab-inline">';
echo '<img src="' . esc_url( $tension_knit ) . '" alt="tension knit icon" />';
echo '</div>';
you'll want an if statement for that (as mentioned in comments) and if it were me I'd do something like:
echo '<div class="tab-inline">';
if(isset($tension_knit) && $tension_knit != "") {
echo '<img src="' . esc_url( $tension_knit ) . '" alt="tension knit icon" />';
}
echo '</div>';
That's going to produce an empty div for you if the $tension_knit isn't there, you'll have to decide if that's what you want or not. Your question asked specifically about getting rid of the image/alt. If you didn't want the div at all in case of empty $tension_knit, then you could just move the "if" up a line and the closing tag down a line. Good luck!

How to echo out a post url within a src attribute as background image in WordPress?

I'm trying to create a conditional statement that checks to see if a post has a thumbnail, and if so, echos out a div that has the post thumbnail as the background image.
<?php if( the_post_thumbnail() ) {
echo '
<div class="post-hero" style="background-image: url(' . wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ');">';
'</div>';
};?>
The problem is that my editor (vsCode) is throwing an error: an unexpected echo. I'm thinking it has to do with the second echo within the url.
I've tried changing echo to print, but that didn't work as well.
I'm stuck, and I'm new to PHP programming. Anything glaring in my code?
There is a error in your echo string and the_post_thumbnail() needs to be replaced by has_post_thumbnail(). See this fixed version:
if(has_post_thumbnail() )
echo '<div class="post-hero" style="background-image: url(\'' . wp_get_attachment_url( get_post_thumbnail_id($post->ID)). '\');"></div>';
}
There were two errors in my code, one answered by #Amacado, and I changed
if( the_post_thumbnail() )
to
if( has_post_thumbnail() )
It worked then. Thanks!

Display advanced custom field (ACF) value from custom post type taxonomy - wordpress

In Wordpress I created a custom post type called "Sports" with a taxonomy called "sport_locations". Using the advanced custom field (ACF) plugin I created fields to show on taxonomy terms. I got everything to work, however I'm having trouble outputting the image I uploaded.
In ACF I have the option for the return value of the image to be: an object, url, or ID. Right now I have it set to object.
Below is the code I have so far. I'm writing this code inside of the single-sports.php . I created a foreach loop and only spitting out the terms associated to that specific sport.
When I do the var_dump I keep getting bool(false). Sorry for the extra commenting out in the code I was trying a bunch of different things and figured I'd leave it in my code as reference
post type = sports
taxonomy = sport_location
acf field = location_image (return value = object)
<?php
$terms = get_the_terms( $post->ID , 'sport_locations' );
//$LocImg = $wp_query->queried_object;
// echo '<pre>';
// echo var_dump($LocImg);
// echo '</pre>';
foreach ( $terms as $term ) {
$term_thumb = get_field('location_image', 'sport_locations'.$term->term_id);
echo '<pre>';
echo var_dump($term_thumb);
echo '</pre>';
echo '<div class="sport-single">';
echo'<img src="' .$term_thumb['url']. '" class="img-responsive">';
//echo '<img src="' .$locationImage[0]. '" class="placeholder" />';
//echo '<img src="' .get_src_picture(get_field("location_image", "sport_locations".$LocImg->term_id), "category"). '" class="img-responsive" />';
//echo '<img src="' .get_field( "location_image", $LocImg->taxonomy . "_" . $LocImg->term_id ). '" />';
echo '<p>'.$term->name .'</p>';
echo '</div>';
}
?>
There is supposed to be an underscore between the term name and the ID. So...
$term_thumb = get_field('location_image', 'sport_locations'.$term->term_id);
...should be...
$term_thumb = get_field('location_image', 'sport_locations_'.$term->term_id);
Alternatively, you can pass the term object...
$term_thumb = get_field('location_image', $term);

How to make the featured image without a fixed position?

I already know how to style the featured image and give it a class, but I don't want it to have a fixed position. Instead, I want it to have a custom position for every post (I set the position manually for every post). How possibly can I achieve that ?
Add this code in your post loop, to make as way you want :
<?php
if ( has_post_thumbnail() )
the_post_thumbnail();
else
echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/default- thumbnail.png' . '" alt="" />';
?>

Why wont all of this string print?

I am just trying to generate a path with something like :
$PhotoName = the_title();
$DestinationFile = 'temp/watermarked/';
$DestinationFile .= $PhotoName;
$DestinationFile .= '.jpg';
the_title(); is a Wordpress function that gets the title of the post. If I echo just $PhotoName I see the name of the post as suspected. However if I echo $DestinationFile it will never print that part of the string so I would see something like temp/watermarked/.jpg, it never prints $PhotoName as part of it.
You need to pass false as the third parameter to get it to return the page title to you.
$PhotoName = the_title('', '', false);
See the WordPress reference for more details.
the_title() is a template tag that must be run within the loop. The title applies filters and prints the title to the screen.
Use get_the_title() to return the value in php.
When using a foreach loop with get_posts() use $post->post_title to get the value. If you want the filters applied: apply_filters( 'the_title', $post->post_title );

Categories