Created
September 4, 2013 17:56
-
-
Save lswilson/6440431 to your computer and use it in GitHub Desktop.
Related Posts (via tags or categories)
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
/** | |
* Setup the in-post related posts list | |
*/ | |
function wds_threatpost_related_posts() { | |
// Get tags, if they exist... | |
$tags = wp_get_post_tags( get_the_ID() ); | |
if ( $tags ) { | |
$tag_ids = array(); | |
foreach( $tags as $single_tag ) $tag_ids[] = $single_tag->term_id; | |
$args = array( | |
'tag__in' => $tag_ids, | |
'post__not_in' => array( get_the_ID() ), | |
'showposts'=> 3, //number of related posts | |
'ignore_sticky_posts'=> 1 | |
); | |
} else { // Or get categories | |
$categories = get_the_category( get_the_ID() ); | |
if ( $categories ) { | |
$category_ids = array(); | |
foreach( $categories as $single_category ) $category_ids[] = $single_category->term_id; | |
$args = array( | |
'category__in' => $category_ids, | |
'post__not_in' => array( get_the_ID() ), | |
'showposts'=> 3, //number of related posts | |
'ignore_sticky_posts'=>1 | |
); | |
} | |
} | |
// Run the query | |
if( $args ){ | |
$my_query = new wp_query( $args ); | |
if( $my_query->have_posts() ) : | |
echo '<div class="related-posts-inner"><h3>'. __( 'Related Posts', 'kasp' ) .'</h3>'; | |
while ( $my_query->have_posts() ) : $my_query->the_post(); | |
if ( $my_query->current_post+1 == $my_query->post_count ) | |
$last = 'last'; | |
else | |
$last = null; ?> | |
<article id="post-<?php the_ID(); ?>" <?php post_class( array( 'secondary-post', $last ) ); ?>> | |
<h4 class="entry-title"> | |
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'kasp' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a> | |
</h4> | |
<div class="post-info"> | |
<span class="date"><?php echo the_time( get_option('date_format') ); ?> , <?php echo the_time( get_option('time_format') ); ?></span> | |
</div> | |
</article><!-- #post --> | |
<?php endwhile; | |
echo '</div>'; | |
endif; | |
wp_reset_postdata(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment