How do you show category titles on a page in wordpress. I want to to show the category titles on my home page which would then link to the first post in that category. I also want to show the image of the first post in that category.

Any way i can do this? i was thinking wp_query?

1 Answer 1


You will need to combine get_categories() and wp_query

First you should get all categories and loop through each one of them. Inside the loop you need a wp_query to get the title and the image of the latest post per category. At last you should reset the post data.

Place the code below inside your homepage template:

$args = array (
    'hide_empty' => 1,
$terms = get_categories( $args );

foreach ( $terms as $term ) {
    $query = new WP_Query( array( 'post_type' => 'post', 'cat' => $term->term_id, 'posts_per_page' => 1 ) );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) { 
            $postlink = get_the_permalink( $query->ID );
            $postimage = wp_get_attachment_url( get_post_thumbnail_id( $query->ID ) );
            echo '<h4><img src="'.$postimage.'"><a href="'.$postlink.'">'.$term->name.'</a></h4>';

// Restore original Post Data
  • @PieterGoosen May I know what is the reason for the downvote?
    – vadims
    Dec 30, 2015 at 13:04
  • Glad I could help @Raj
    – vadims
    Dec 30, 2015 at 19:53

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.