Support » Developing with WordPress » Need to add a post’s featured image to a section on the frontpage

  • It looks like I will need to create custom PHP code for this and either add it to my includes directory within a custom PHP file, or to the functions.php file.

    I need the ability to use a featured image in a post and be able to also dynamically display that same image in a gallery on the frontpage.

    I am not a PHP master by any means, but I know a bit to get around.

    My guess is I would need something comparable to this:

    if ( has_post_thumbnail() ) {     
        echo '<div style='background-image: url(' . get_the_post_thumbnail_url() . ');></div>';

    How would I get the featured image to display within in a certain section on the frontpage?

    There’s a “Projects” custom post type where the actual posts are created. There are fields within this post type created using ACF. There’s a “Project Featured” section in this custom post type that offers a toggle to turn off or on “Featured”. If the toggle is on, that is when the featured image should be used.


    • This topic was modified 1 week, 2 days ago by ronthewebdev.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    How is your front page’s content generated? Specifically what sort of element would be displaying these images? Block? Widget (sort of same as blocks these days, but they go in a specific area of the page separate from page content)? Or is it generated by template code?

    The type of element dictates how the images are actually output, but the code to get the images is more or less identical in any case. Your code would likely need to query for the posts whose featured images you want to display. You could query for the image attachments directly, but the query criteria you’d likely want to use probably belong to the posts, not the attachments.

    You only need the IDs of the relevant posts in order to get their featured images. You can loop through the results much like you’d do for the posts themselves, except you’d only output their featured images by using get_the_post_thumbnail() to get the image’s HTML. (you can get any registered image size with this, doesn’t have to be thumbnail size)

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.