0

I'm trying to list categories for my posts on the home page with each of their posts under them. I'm not really sure how to go about doing this.

Example of what I want.

Category 1

Post 1 - Post 2 - Post 3 - Post 4

Category 2

Post 1 - Post 2 - Post 3 - Post 4

Category 3

Post 1 - Post 2 - Post 3 - Post 4

EDIT 2:

Got this to work.

<?php
    $args = array(
        'orderby' => 'name',
        'order' => 'ASC'
    );

    $categories = get_categories($args);

    foreach( $categories as $category ) { 
        echo '<h1><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h1>';

        global $post;

        $args = array(
            'posts_per_page' => 4,
            'offset'=> 0,
            'category' => $category->term_id
        );

        $posts = get_posts( $args );

        foreach( $posts as $post ) {
            echo '
                <a href="' . $post->guid . '" title="' . $post->post_title . '">
                    ' . get_the_post_thumbnail($page->ID, 'small') . '
                </a>
            ';
        }
    } 
?>
  • Edited post with new code – Cory Nickerson Jul 9 '13 at 23:40
  • Please post working code as answer, so you can mark it as solution later on. That's the idea of the site :) – kaiser Jul 27 '13 at 0:23
1

I would say you have to run queries by category name or category ID, use WP_Query:

$query = new WP_Query( 'cat=cat_ID', 'posts_per_page' => 4 );

Maybe get_posts() is more appropriate in this context (array of posts)

EDIT: in your edit use get_the_post_thumbnail( $post->ID, 'thumbnail')instead, small is not a default size

EDIT2: it is better to use the_title_attribute() instead of the_title() if is in an HTML attribute :

title="'.the_title_attribute($post->ID).'"

In case you use quote in your title it will escape it so it won't break HTML

  • I updated the post. I need to get the thumbnails working it seems. Not sure how to get the thumbnails with get_posts() – Cory Nickerson Jul 9 '13 at 23:44
  • I've edited code. With get_posts() you can get a lot, do not worry. – JMau Jul 10 '13 at 11:26
0

The following works.

<?php
    $args = array(
        'orderby' => 'name',
        'order' => 'ASC'
    );

    $categories = get_categories($args);

    foreach( $categories as $category ) { 
        echo '<h1><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h1>';

        global $post;

        $args = array(
            'posts_per_page' => 4,
            'offset'=> 0,
            'category' => $category->term_id
        );

        $posts = get_posts( $args );

        foreach( $posts as $post ) {
            echo '
                <a href="' . $post->guid . '" title="' . $post->post_title . '">
                    ' . get_the_post_thumbnail($page->ID, 'small') . '
                </a>
            ';
        }
    } 
?>

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.