Are you looking for a way to display thumbnails from the “featured image” field in the post editor? While there’s probably a plugin for this, we have created a quick code snippet that you can use display thumbnails in a list from a specific category in WordPress.
Instructions:
All you have to do is add this code to your theme’s single.php file:
<?php $thumbnails = get_posts('numberposts=6&cat=3'); foreach ($thumbnails as $thumbnail) { if ( has_post_thumbnail($thumbnail->ID)) { echo '<li class="s-uppdrag-margin">'; echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">'; echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail'); echo '</a>'; echo '</li>'; } } ?>
You need to replace your post ID and category ID in the above code to display the list of thumbnails.
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 19 best WordPress plugins for non-profits and how to set up Google Analytics eCommerce tracking for WooCommerce.
Comments Leave a Reply