Created
August 3, 2015 22:37
-
-
Save RadGH/e4631a3d39ad7082fd67 to your computer and use it in GitHub Desktop.
Yet Another Related Posts Plugin (YARPP) - Template with post thumbnails and better markup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
YARPP Template: Image Previews | |
Description: Four columns of posts, each with a photo. | |
Author: radgh (RadleyGH@gmail.com) | |
*/ | |
// Put your placeholder image ID here, from the media tab in the dashboard, or leave it false to disable. | |
$placeholder_media_id = false; | |
$placeholder = false; | |
if ( $placeholder_media_id ) $placeholder = wp_get_attachment_image_src( $placeholder_media_id, 'thumbnail' ); | |
?> | |
<h3>Related Posts</h3> | |
<div class="yarpp-post-container yarpp-image-previews"> | |
<?php | |
if (have_posts()): | |
while(have_posts()): the_post(); | |
$class = array('yarpp-post'); | |
$image_id = get_post_thumbnail_id(); | |
$image = false; | |
$alt = ""; | |
if ( $image_id ) $image = wp_get_attachment_image_src( $image_id, 'thumbnail' ); | |
if ( $image ) { | |
$class[] = 'has-image has-featured-image'; | |
// Get alt text, prefer the alt tag, fall back to the title, fall back to the content | |
$alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true ); | |
if ( !$alt ) $alt = get_the_title($image_id); | |
if ( !$alt ) $alt = wp_strip_all_tags( get_the_content($image_id) ); | |
}else{ | |
if ( !$image ) { | |
$class[] = 'has-image has-placeholder'; | |
$image = $placeholder; | |
}else{ | |
$class[] = 'no-image'; | |
} | |
} | |
$excerpt = wp_trim_words( strip_shortcodes(wp_strip_all_tags(get_the_content())), 15 ); | |
?> | |
<div class="<?php echo esc_attr(implode(' ', $class)); ?> "> | |
<?php if ( $image ) { ?> | |
<div class="yarpp-post-image"> | |
<a href="<?php the_permalink(); ?>" title="Continue reading <?php echo esc_attr(get_the_title()); ?>"> | |
<img src="<?php echo esc_attr($image[0]); ?>" alt="<?php echo esc_attr($alt); ?>" /> | |
</a> | |
</div> | |
<?php } ?> | |
<div class="yarpp-post-content"> | |
<div class="yarpp-post-title"> | |
<a href="<?php the_permalink(); ?>" title="Continue reading <?php echo esc_attr(get_the_title()); ?>"><?php the_title(); ?></a> | |
</div> | |
<div class="yarpp-post-text"> | |
<?php echo esc_html($excerpt); ?> | |
</div> | |
</div> | |
</div> | |
<?php | |
endwhile; | |
else: | |
?> | |
<p>No related posts.</p> | |
<?php | |
endif; | |
?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CSS: