/ Published in: PHP

This will show a number of recent posts along with their associated featured image (thumbnail) if they have one. The template code asks for the 2 most recent posts with the custom post type of "news." Change the quantity and post type as needed. This also uses a custom excerpt generator, which isn't necessary except that this particular code references it.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // custom excerpt generator. put into functions.php. function flexcerpt($excerpt = '', $excerpt_length = 50, $readmore = "Read more»", $tags = '<a>', $permalink = null) { global $post; $excerpt = strip_shortcodes($excerpt); if ($permalink === null) { $permalink = get_permalink(); } // $excerpt .= ' ... <a href="' . $permalink . '">' . $readmore . '</a>'; // return $excerpt; } else { //return $excerpt; } $excerpt .= '... <a href="' . $permalink . '">' . $readmore . '</a>'; return $excerpt; } ?> <!-- this goes into the template file. it is using a custom post type called "news" and can be changed to whatever is needed. --> <?php foreach ( $news as $post ): ?> <?php setup_postdata($post); ?> <div class="home-news-article <?php echo (has_post_thumbnail() ? 'with-thumbnail' : 'no-thumbnail') ?>"> <?php if (has_post_thumbnail()): ?> <div class="home-news-thumbnail"> <?php the_post_thumbnail( 'news-thumbnail' ); ?> </div><!--.home-news-thumbnail--> <?php endif ?> <div class="home-news-content"> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <?php echo flexcerpt( $post->post_excerpt ); ?> <?php else: ?> <?php echo flexcerpt( get_the_content() ); ?> <?php endif ?> </div><!--.home-news-content--> </div><!--.home-news-article--> <?php endforeach; ?>
Comments
