WordPress: visualizzare i post correlati di ciascun autore

WordPress: visualizzare i post correlati di ciascun autore

Per ciascun autore di WordPress possiamo visualizzare 5 articoli correlati all'interno dell'articolo corrente scritto da tale autore. Vediamo come fare.

Aggiungete il seguente codice al file functions.php:


function get_related_author_posts() {
    global $authordata, $post;
    $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );
    $output = '<ul>';
    foreach ( $authors_posts as $authors_post ) {
        $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
    }
    $output .= '</ul>';
    return $output;
}

Quindi aggiungete la funzione al file single.php del vostro tema:


<div id="author-related">
	<?php echo get_related_author_posts(); ?>
</div>
Torna su