NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Intermediate

---- Last modified : Sep 18 2022

Estimated reading time : 3 minutes, 42 seconds - 300 words

Here’s a quick and easy tutorial to display posts published by your WordPress authors. Consult the Codex Author Templates.

There are two ways to go about it. Both are efficient methods of doing it, but the second one is cleaner and easier.

First solution

This query will allow you to display posts from a WordPress author.

If for example, you want to display your author’s ads, simply add this argument: post_type => ads.

For the get_the_post_thumbnail, we simply choose the size desired. You choose from thumbnail, medium or custom size with add_image_size

<?php
//////////////////////////////////////
// Fonction post reliés author
///////////////////////////////////////
function get_author_posts() {
global $authordata, $post;

// Nous creons une variable qui stocke tous les posts par auteur en excluant l'article consulté sur la page en affichant en aléatoire et un certain nombre
$authors_posts = get_posts( 
array( 
'category__in'            => wp_get_post_categories($post->ID), 
'author'                  => $authordata->ID, 
'post__not_in'            => array( $post->ID ), 
'posts_per_page'          => 6, 
'orderby'                 => 'rand' ) );

// Nous commencons a afficher nos resultats
$output =''; 
foreach ( $authors_posts as $authors_post ) { // Nous allons chercher le title du post 
	$parent_title = get_the_title($authors_post->ID); 
	$excerpt = get_the_excerpt($authors_post->ID); 
	// Nous commencons a afficher nos resultats 
	$output .= ''; 
	$output .= get_the_post_thumbnail($authors_post->ID,’thumbnail’); 
	$output .= '' . apply_filters( ‘the_title’, $authors_post->post_title, $authors_post->ID ) . ''; 
	$output .= ''; 
} 
$output .= ''; 
// On retourne tous les résultats 
return $output; 
}
?>

You’ll notice that in the category__in => wp_get_post_categories($post->ID) query allows you to display post displayed in a category.

For example, we will display this in the single.php or single-post-type.php template.

<?php echo get_author_posts(); ?>

The second solution

You’ll find the function below. It is easier to use given that it uses arguments in a table and returns results with the $authors_posts variable.

You’ll also notice that we’re using a Post_type which matches the posts, but you can also use your own POST_TYPE and add additional arguments.

The post__not_in => array( $post->ID ) allows users to exclude post or single post type of the current post given that we don’t want to see it in the linked posts.

We could also add the category posts related to the author’s posts by using this argument: category => category_id. Simply replace category_id by your own category’s ID.

function get_theme_author_posts( $items = 1 ) {
global $authordata, $post;
$authors_posts = get_posts(
array(
'author' =&gt; $authordata-&gt;ID,
'post__not_in' =&gt; array( $post-&gt;ID ),
'posts_per_page' =&gt; $items,
'post_type' =&gt; 'post'
)
);
return $authors_posts;
}

This will be what we displayed in the single.php or single-post-type.phptemplate.

<?php $related_posts = get_theme_author_posts(3); foreach( $related_posts as $post ) : setup_postdata( $post ); ?>

You’ll also see that default permalink is author.

Ex : http://www.viamultimedia.ca/author/tony

If you want to change the authors’ permalink, simply use wp_rewrite.

//////////////////////////////////////
// Fonction rewrite url author
///////////////////////////////////////
add_action('init', 'via_author_base');
function via_author_base() {
global $wp_rewrite;
$author_slug = 'auteur'; // change slug name
$wp_rewrite-&gt;author_base = $author_slug;
}

This will result in a new permalink slug.

Ex : http://www.viamultimedia.ca/wp-authors/tony

Finally, you can customize the CSS of your authors’ posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>