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.
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: ', ' » ' );// with link?> »-->
<!-- Term(s): <?php //echo join(', ',wp_get_post_terms($post->ID, $taxonomy, array("fields" => "names")));// without link?> » -->
<?php echo '<a href="'.$get_term_link.'">'.$term_name.'</a>';?> »
Last Post: <a href="<?php echo $permalink;?>"><?php echo $post_title;?></a>
</li>
<?php
endwhile;
endforeach;
echo '</ul>';
?>