1

I have a code for display recent posts:

<?php $args = array( 'numberposts' => '5' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ echo '<h2><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </h2> '; echo '<p>' . date_i18n('d F Y', strtotime($recent['post_date'])) .'</p> '; // what code here? } wp_reset_query(); ?>

now I want to display category name and link also. What kind of code should I use in here? I try some but without effect...

Thx :)

1

You can use get_the_category_list() to output a comma-separated list of links to categories:

<?php
    $args = array( 'numberposts' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<h2><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </h2> ';
        echo '<p>' . date_i18n('d F Y', strtotime($recent['post_date'])) .'</p> ';
        echo get_the_category_list( ', ', '', $recent["ID"] );
    }
    wp_reset_query();
?>
| improve this answer | |
  • Ok, but it's shows only one category, independently of post :/ – Damian P. Jul 25 '17 at 14:49
  • Sorry, I left out passing the post ID to the function. I've updated my answer. – Jacob Peattie Jul 25 '17 at 14:52
0

i know This is the different code but works as you want hope so,you can try

<?php
    $myposts = array( 'post_type' => 'post','posts_per_page' => 5); 
    $all_post = new WP_Query($myposts);
    if ( $all_post->have_posts() ) :
        while ( $all_post->have_posts() ) :
            $all_post->the_post();

            echo get_the_title();
            echo '<p>' . 'Category Name:' . get_the_category_list($post->ID ) . '</p>';
        endwhile;
    endif;
    wp_reset_postdata();
?>
| improve this answer | |

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.