0

I am trying to display post category in a foreach loop. I have all the elements displaying properly except for the category. Here is my function:

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 ) {
    $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>'.the_category('', '', $authors_post->ID).'
    </li>';
}
$output .= '</ul>';

return $output;}

Follow the following link to see the current output. https://www.evernote.com/shard/s250/sh/0e3c6bf9-5676-4d56-acdf-ee6f53285acf/fa43094ca54f018c0e517b4e678d4231

How can I wrap the_category function under html output?

3

When the foreach loop is from get_posts(), you need to run setup_postdata() for each post if you want to use themplate tags inside the loop like the_category(), if not you can get unexpected behaviour. Also, you will need to run wp_reset_postdata() after the foreach loop. Note that the_category and similar template tags actually displays/echoes the output, so, if you want to concatenate the output in a string you should use get_the_category() instead.

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 ) {

        // Build a comma separated categories list
        // You can customize as needed
        // or use get_the_category_list() for quick and delimited list of categories
        // http://codex.wordpress.org/Function_Reference/get_the_category_list
        $categories = get_the_category($authors_post->ID);
        $categories_string = '';
        $separator = ', ';
        if($categories) {
            foreach($categories as $category){
                $categories_string .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$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', $authors_post->ID ).'</span>'.$categories_string.'
        </li>';
    }

    $output .= '</ul>';

    return $output;
}
  • I got the following outpout World’s Largest Economies 2014 04.30.14 Array – Hussain Ansari May 1 '14 at 10:05
  • get_the_category() turned into Array – Hussain Ansari May 1 '14 at 10:06
  • I forget get_the_category() returns an array. See edtired answer. – cybmeta May 1 '14 at 10:36
  • Works but without link. – Hussain Ansari May 1 '14 at 10:48
  • As the comment inside the loop says, you can customize the $categories_string as you want or need. I've added each category as link to the category's archive page. Please, note that your question is already answered and working; I can not provide the exact output you want because you have not ask about it, your question was about "Display post category inside a foreach loop" in general. If you need a specific output, please, say to us. – cybmeta May 1 '14 at 14:56
0

the_category() function must be used inside the loop. You can use get_the_category() function instate.

So the new code is

      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 ) {
        $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>'.get_the_category_list(', ', '', $authors_post->ID).'
        </li>';
    $output .= '</ul>';

    return $output;
    }  
  • Please provide a proper structured answer. This not an answer but a comment. Please see the structure of @cybnet answer – Pieter Goosen May 1 '14 at 9:55
  • The proposed code won't work if the post is attached to multiple categories beacuse it outputs only the name of the first category object returned by get_the_category() – cybmeta May 1 '14 at 10:41
  • Sorry, works but without link. – Hussain Ansari May 1 '14 at 10:49
  • @Hussain Ansari I added the category link also – Maidul May 1 '14 at 11:06
  • @cybnet I used the get_the_category_list() function now to get all the category name with link – Maidul May 1 '14 at 11:17

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.