You, or whoever wrote the code, is misunderstanding the proper usage of set_post_thumbnail_size( 250, 104 );
.
https://developer.wordpress.org/reference/functions/set_post_thumbnail_size/
It’s used in the wrong context here. It should be called on any and every request. It sets the defined “post-thumbnail” image size for all featured images. It’s generally called from a ‘after_setup_theme’ action hook, never a shortcode handler.
Setting that size only works well if the featured images are uploaded after that size is set. Earlier images likely have been scaled to the wrong sizes. The right CSS can compensate for mismatches in size, but it’s better if the correct size is available for use. One way to get older images to have the right size available is to use one of the plugins that regenerates older images at the currently registered sizes.
If you want featured images displayed at a size other than the normal default featured image size, specify the desired size in the get_the_post_thumbnail()
call. Do not use set_post_thumbnail_size()
for occasional deviations from the theme’s default size.
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/
I should have clarified further:
* Firstly, I know virtually nothing about PHP coding and have jumped in the deep end on this.
* The result I am trying to achieve is to use this custom function to REPLACE a plugin so that I can delete the plugin and reduce the loading times on my site.
* Currently the above code is pulling a featured image that is not attached to/associated with any of the posts in the listing produced.
* When I tried changing the code to $featured_image = the_post_thumbnail( array( 'class' => 'alignleft' ) );
it then produced a page with all the right post thumbnails but they were all clumped together at the top of the page.
* The Code is working correctly in listing the posts but it is not quite positioning the images where they should be placed when I use $featured_image = the_post_thumbnail( array( 'class' => 'alignleft' ) );
It looks as though they are being positioned where they would be if they were rendering on a post as opposed to within a listing of results on a page.
* I need the $featured_image to display adjacent to their associated excerpts.
What am I missing in understanding?
-
This reply was modified 3 years, 1 month ago by clairedelune.
-
This reply was modified 3 years, 1 month ago by clairedelune.
I am wondering if I need to work further in the line: $content .= '<p class="excerpt">'.$featured_image.' '.get_the_excerpt(). '</p>';
the_post_thumbnail()
outputs the related image tag. That’s an issue in shortcode handlers because there must be no output. All output must be collected into a single variable which is then returned. Everything appearing at the top of the page is a classic sign of inappropriate output.
The function that returns the featured image tag so it can be assigned to a variable is get_the_post_thumbnail()
. Functions in WP are often declared both with and without the get_
. One returns and the other echoes out.
The line concatenating featured image and excerpt to $content is OK if it results in the HTML you want. Final appearance on the page is up to the related CSS code.
Thank you for responding again.
I will try working with some CSS and see how I go with that.
Your assistance is greatly appreciated.
For posterity, in case anyone else needs a similar solution, I found an updated version of the script that is written to work better in the blocks system:
https://www.wpbeginner.com/wp-tutorials/how-to-display-recent-posts-from-a-specific-category-in-wordpress/
So, using the Code Snippets plugin, I modified the code to suit my objective including with properly positioned post featured images:
function faq_postsbycategory() {
// the query
$the_query = new WP_Query( array(
'category_name' => 'faq',
'posts_per_page' => 10,
'order' => 'ASC',
'orderby' => 'link'
) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<div class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<p>';
$string .= '<b><a href="' . get_the_permalink() . '"rel="bookmark">' . get_the_post_thumbnail($post_id, array( 200, 100) ) . get_the_title() . '</a></b><br><h7>Posted: ' . get_the_date() . '</h7><br>' . get_the_excerpt() . '</p>';
}
else {
// if no featured image is found ADD EXCERPT CODING
// $string .= '<p><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() . '</a><br><h7>Posted: ' . get_the_date() . '</h7><br>' . get_the_excerpt() . '</p>';
}
}
} else {
// no posts found
}
$string .= '</div>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('faq-posts', 'faq_postsbycategory');
Here is the styling I have used:
.postsbycategory img {
float: left;
margin-left: 5px;
margin-right: 20px;
margin-top: 5px;
margin-bottom: 5px;
}
.postsbycategory p {
padding-bottom: 30px;
}
Lastly, as posted earlier, I enclosed the [faq-posts]
shortcode within an additional <div></div>
to be certain that everything is contained within its own discrete container.
Thank you again @bcworkz.
Don’t you just love coding? The day after I got this working something triggered a fatal error on my site when I cloned the above and modified it for another category.
Working again but now it spits out the comments forms and responses on the last post in the generated list.
How can I exclude the response box and fields and all the submitted comments from being displayed at the bottom of the page the shortcode is used on?
Again for posterity, additional CSS to hide the unnecessary / unwanted display of comments.
.page-id-9999 #comments {
display:none;
}
Replace the number 9999 in the page-id- to your own page. Find the page ID per the instructions here: 8 Proper Ways to Hide Elements on Your Website With CSS (2021)
Happy coding.