0

I want get all categor and after that i want get 5 post per category. but my code not work! http://pastebin.com/vxDSGGZk

<?php
    $categories=get_categories('');
    foreach($categories as $category) { 
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        echo '<p> Cat ID: '. $category->cat_ID. '</p>';

        // The Query
        $id_cat = $category->cat_ID;}
        $args = array(
            'cat' =>  $id_cat,
            'posts_per_page' => 5,
        );
        query_posts( $args );

        // The Loop
        while ( have_posts() ) : the_post();
            echo '<li>';
            the_title();
            echo '</li>';
        endwhile;
    }

    // Reset Query
    wp_reset_query();
?>

please help me! thanks! P/s:this is the first time i ask on here and i can't paste code in here. please check link pastebin.

1
  • Please do not link code from external sources - always put everything one needs to understand your problem into your question. Thanks.
    – kaiser
    Commented Jul 25, 2013 at 9:07

1 Answer 1

0
<?php
$categories=get_categories('');
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Cat ID: '. $category->cat_ID. '</p>';

    // The Query
    $id_cat = $category->cat_ID;
    $args = array(
        'cat' =>  $id_cat,
        'showposts' => 5,
    );
    query_posts( $args );

    // The Loop
    while ( have_posts() ) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
}

// Reset Query
wp_reset_query();
2
  • try above code , there was a error in loop.
    – Vikram
    Commented Jul 25, 2013 at 10:05
  • Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Commented Jul 25, 2013 at 10:24

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.