Created
February 28, 2014 18:19
-
-
Save jeherve/9276637 to your computer and use it in GitHub Desktop.
[Jetpack] Related Posts: retrieve Related Posts manually, and return post titles
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 | |
function jeherve_custom_related( $atts ) { | |
$posts_titles = array(); | |
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) { | |
$related = Jetpack_RelatedPosts::init_raw() | |
->set_query_name( 'jeherve-shortcode' ) // Optional, name can be anything | |
->get_for_post_id( | |
get_the_ID(), | |
array( 'size' => 3 ) | |
); | |
if ( $related ) { | |
foreach ( $related as $result ) { | |
$related_post = get_post( $result[ 'id' ] ); | |
$posts_titles[] = $related_post->post_title; | |
} | |
} | |
} | |
return implode( ', ', $posts_titles ); | |
} | |
add_shortcode( 'jprel', 'jeherve_custom_related' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working grate. Is there a way to return posts url adress instead of post titles, names, date, etc?