0

I have the following code to show current author post lists with post thumbnail, post title, post date and post category.

function my_get_display_author_posts() {
global $authordata, $post;

$authors_posts = get_posts( array( 'author' => $authordata->ID,'posts_per_page' => 6, 'post__not_in' => array( $post->ID ) ) );

$output = '<ul>';

foreach ( $authors_posts as $authors_post ) {

    setup_postdata($authors_post);

    // Build a comma separated categories list
    // You can customize as needed
    $categories = get_the_category();
    $categories_string = '';
    $separator = ', ';
    if($categories) {
        foreach($categories as $category){
            $categories_string .= $category->cat_name.$separator;
        }
        $categories_string = trim($categories_string, $separator);
    }

    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $authors_post->ID ), 'related-author' );
    $output .= '<li>
    <a class="title" href="' . get_permalink( $authors_post->ID ) . '">
    <strong>' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</strong>
    <img src="'.$image[0].'"> 
    </a>
    <span>'.get_the_time('m.d.y').'</span> '.$categories_string.'
    </li>';
}

wp_reset_postdata();

$output .= '</ul>';

return $output;}

Please follow below link to see the output

https://www.evernote.com/shard/s250/sh/ba2c1f8f-c06e-44ef-b004-c0432b7d3a5f/12febb0db08fd9a749eee779051cb007/deep/0/Fullscreen-02-05-14-3-35-pm.png

I want to show relevant post categories with category link.

Thanks!

  • You can replace, $categories_string .= $category->cat_name.$separator; in for loop with below, $categories_string . = '<a href="'.get_category_link($category->term_id ).'">'.$category->cat_name.'</a>' . $separator; This may solve the purpose. – WisdmLabs May 28 '15 at 4:56
  • Any feedback on this issue? Please see What should I do when someone answers my question? – Pieter Goosen Jul 13 '15 at 19:01
0

You are fighting with the post globals, like $post, and are not getting them set correctly. Create a proper Loop using WP_Query and use Core template tags where possible and the problem will go away.

$authors_posts = new WP_Query(
  array( 
    'author' => 7,
    'posts_per_page' => 6,
    'post__not_in' => array( $post->ID ) 
  ) 
);

$output = '<ul>';

while ($authors_posts->have_posts()) {
  $authors_posts->the_post();

  // Build a comma separated categories list
  // You can customize as needed
  $categories_string = get_the_category_list();

  $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'related-author' );
  $output .= '<li>
    <a class="title" href="' . get_permalink() . '">
    <strong>' . get_the_title() . '</strong>
    <img src="'.$image[0].'"> 
    </a>
    <span>'.get_the_time('m.d.y').'</span> '.$categories_string.'
  </li>';
}

wp_reset_postdata();

$output .= '</ul>';

Also, note that your code was far more complex than it needed to be and basically recreated, in limited form, Core functionality.

0

sintax : <?php get_the_category_list( $separator, $parents, $post_id ); ?>

Code if u want with ',' seperated:

<?php echo get_the_category_list( ',', '', $post->ID ); ?>

Or if custom post taxonomy category get using: artist_category is custom post taxonomy

<?php echo get_the_term_list($post->ID, 'artist_category','<li>',',</li><li>','</li>'); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.