Last active
December 1, 2022 20:16
-
-
Save ajaydsouza/968b24a052e858bf8926 to your computer and use it in GitHub Desktop.
Contextual Related Posts plugin API examples
This file contains hidden or 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 | |
/* | |
* 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(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment