1

How I can display the last post by each category?

Point is to display the last post on the page for all categories, I need to show them in front as normal <ul><li> list.

Any idea?

Thanks.

1 Answer 1

1

Try this

<?php
$post_type = "post";
$taxonomy = "category";
$terms = get_terms(
    array(
        'taxonomy' => $taxonomy,
        'hide_empty' => 0,
        'hierarchical'=> 1,
    )
);

echo '<ul>';

foreach($terms as $term):

    $term_id = $term->term_id;
    $term_name = $term->name;

    $query = array(
        'post_type' => $post_type, 
        'post_status' => 'publish',
        'posts_per_page' => 1,
    );
    $query['tax_query'] = array(
        array(
            'taxonomy' => $taxonomy,
            'terms' => $term_id
        )
    );
    $posts = new WP_Query($query);

    while($posts->have_posts()):
        $posts->the_post();
        $permalink = get_permalink($post->ID);
        $post_title = $post->post_title;
        $get_term_link = get_term_link($term_id);
    ?>
        <li>
        <!--<?php //echo the_terms( $post->ID, $taxonomy, 'Term: ', ' &raquo; ' );// with link?> &raquo;-->
        <!-- Term(s): <?php //echo join(', ',wp_get_post_terms($post->ID, $taxonomy, array("fields" => "names")));// without link?> &raquo;  -->
        <?php echo '<a href="'.$get_term_link.'">'.$term_name.'</a>';?>  &raquo; 
        Last Post: <a href="<?php echo $permalink;?>"><?php echo $post_title;?></a>
        </li>
    <?php
    endwhile;

endforeach;

echo '</ul>';       
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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