1
votes

I'm trying to make a list of my recent posts that also show what category they are in right now I have

<?php   $args = array(  'numberposts' => 30)  ;
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>
    <a href="'.get_permalink($recent["ID"]).'"  title="Look'.esc_attr($recent["post_title"]).'"   > '.$recent["post_title"].'</a>   </li> ';    } ?>

this displays the post but I would like it to also display the category name.

Any help would be great,

Thanks

4
I tried adding a variable $cats but when it displays the page it prints out array <?php $args = array( 'numberposts' => 10) ; $cats = get_the_category(); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ) { echo '<li> <a href="'.get_permalink($recent["ID"]).'" title="Look'.esc_attr($recent["post_title"]).'" > '.$recent["post_title"].'</a> '.$cats.' </li> '; } ?> - Gavin Morter

4 Answers

2
votes
$cats = get_the_category($recent["ID"]);
$cat_name = $cats[0]->name; // for the first category

You can try this inside the loop (if you have multiple categories)

$cats = get_the_category($recent["ID"]);
foreach($cats as $cat)
{
    echo $cat->name." ";
}
1
votes

I was able to get this to work using the following.

$cats[0]->name." "

So in the recent posts loop you can use it like this:

$args = array('numberposts' => 5, 'category' => '4,5' );
$recent_posts = wp_get_recent_posts( $args );    

foreach( $recent_posts as $recent ){
   $cats = get_the_category($recent["ID"]);
   echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'">' .   $cats[0]->name." " . $recent["post_title"].'</a> </li> ';
}
0
votes

I was able to get a list that displays the category then the title by using the following.

 <?php
  $recentPosts = new WP_Query();
  $recentPosts->query('showposts=30');?>
 <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
 <?php the_category(" "); ?>-<a href="<?php the_permalink()?>">  <?php the_title(); ?></a> 
 <?php endwhile; ?><?php wp_reset_query()?>

I was never able to get the following code to work inside my original code It would always show up as "array" or Null (i'm guessing I just did not know the proper way to write it in) I was able to get it to show the category if I created a single post and just wanted to show the category with nothing else.

 $cats = get_the_category($recent["ID"]);
 foreach($cats as $cat){
 echo $cat->name." "; }
0
votes

I used the answer from Jaco, but

    $cats[0]->name

gave me the first category from the array on each post. The adjustment I made was to use an incremental operator in my code and all is well.

A much simplified example:

    $recent_posts = wp_get_recent_posts( $args );

                    foreach( $recent_posts as $recent ){
                        $i = 0;
                        $cats = get_the_category($recent["ID"]);
                        echo $cats[$i]->name;
                        $i++;
                    }

                    wp_reset_query();