How to make the featured image without a fixed position? - php

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="" />';
?>

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!

WordPress get post thumbnail and wrap it in permalink

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' ) . '"/>';
}
?>

Wordpress isn't letting me hide a custom field in a div?

This code must be used to output my images in a wordpress layout:
<?php
$temp_content = explode(" ",substr(strip_tags(get_the_content()),0,175));
$temp_content[(count($temp_content)-1)] = '';
$new_content = implode(" ",$temp_content);
?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('home-post',
array('alt' => 'post image',
'class' => '', 'title' =>
'<div class="home_post_content">
<h4>
<a href="' . get_permalink() . '">
' . get_the_title() . ''. $the_date .'
</a>
</h4>'));
I want to put the_field('event_date') in just after get_the_title. However whenever I do, it merely outputs the date either above or below the code, with absolutely no formatting around it.
You've got a host of issues going on here depending on your css. Remember headings are block level elements (they take up a whole line) and anchors can be too. I would map out the html I'm trying to create and determine what needs to be inline and sort out the parent/child relationships. I'm not sure this is an issue with Wordpress output.
Post the html that is being outputted and the relevant css.
You should return the value:
get_the_field( 'event_date' )

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