ajaydsouza
2/10/2015 - 10:53 AM

Contextual Related Posts plugin API examples

Contextual Related Posts plugin API examples

<?php
/**
 * Related posts file to use with Hueman theme
 * https://wordpress.org/themes/hueman/
 *
 */

global $post;

$crp_post_meta = get_post_meta( $post->ID, 'crp_post_meta', true );

if ( isset( $crp_post_meta['crp_disable_here'] ) ) {
	$crp_disable_here = $crp_post_meta['crp_disable_here'];
} else {
	$crp_disable_here = 0;
}

if ( ! $crp_disable_here ) {
	$scores = get_crp_posts_id( array(
		'postid' => $post->ID,
		'limit' => 3
		) );

	$posts = wp_list_pluck( (array) $scores, 'ID' );

	$args = array(
		'post__in' => $posts,
		'posts_per_page' => 3,
		'orderby' => 'post__in',
		'ignore_sticky_posts' => 1
	);

	$related = new WP_Query( $args );

}

?>

<?php if ( isset( $related ) && $related->have_posts() && ! $crp_disable_here ): ?>

<h4 class="heading">
	<i class="fa fa-hand-o-right"></i><?php _e('You may also like...','hueman'); ?>
</h4>

<ul class="related-posts group">

	<?php while ( $related->have_posts() ) : $related->the_post(); ?>
	<li class="related post-hover">
		<article <?php post_class(); ?>>

			<div class="post-thumbnail">
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
					<?php if ( has_post_thumbnail() ): ?>
						<?php the_post_thumbnail('thumb-medium'); ?>
					<?php elseif ( ot_get_option('placeholder') != 'off' ): ?>
						<img src="<?php echo get_template_directory_uri(); ?>/img/thumb-medium.png" alt="<?php the_title(); ?>" />
					<?php endif; ?>
					<?php if ( has_post_format('video') && !is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-play"></i></span>'; ?>
					<?php if ( has_post_format('audio') && !is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-volume-up"></i></span>'; ?>
					<?php if ( is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-star"></i></span>'; ?>
				</a>
				<?php if ( comments_open() && ( ot_get_option( 'comment-count' ) != 'off' ) ): ?>
					<a class="post-comments" href="<?php comments_link(); ?>"><span><i class="fa fa-comments-o"></i><?php comments_number( '0', '1', '%' ); ?></span></a>
				<?php endif; ?>
			</div><!--/.post-thumbnail-->

			<div class="related-inner">

				<h4 class="post-title">
					<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
				</h4><!--/.post-title-->

				<div class="post-meta group">
					<p class="post-date"><?php the_time('j M, Y'); ?></p>
				</div><!--/.post-meta-->

			</div><!--/.related-inner-->

		</article>
	</li><!--/.related-->
	<?php endwhile; ?>
	<?php wp_reset_postdata(); ?>

</ul><!--/.post-related-->
<?php endif; ?>

<?php wp_reset_query();
<?php
/*
 * This example fetches the related posts for the current post ID and displays the output in a custom format restricted by the 'news' category 
 *
 */
 
if ( function_exists( 'get_crp_posts_id' ) ) {
	global $post;
	$scores = get_crp_posts_id( array(
		 'postid' => $post->ID,
		'limit' => 7 
	) );
	$posts = wp_list_pluck( (array) $scores, 'ID' );
	$args = array(
		 'post__in' => $posts,
		'posts_per_page' => 7,
		'category_name' => 'news',
		'ignore_sticky_posts' => 1 
	);
	$my_query = new WP_Query( $args );
	if ( $my_query->have_posts() ) {
		while ( $my_query->have_posts() ) {
			$my_query->the_post();
			echo '<a href="' . get_permalink( get_the_ID() ) . '">';
			the_title();
			echo '</a>';
			wp_reset_postdata();
		}
	} else {
	}
	wp_reset_query();
}
?>