Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i had been banging my head for almost a day but could not resolve it. i need to show one post from 4 xx categories here is my code

                <?php
                $cat_args = array(
                  'orderby' => 'name',
                  'order' => 'ASC',
                  'include' => '44,45,46,51'
                );

                $fcategories =   get_categories($cat_args); 

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

                     $post_args = array(
                      'showposts' => 1,
                      'category' => $fcategory->cat_ID
                    );

                    $fposts = get_posts($post_args);

                foreach($fposts as $fpost) : setup_postdata($fpost);    ?>
                        <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
                    <?php

                    echo '<dd class="view-all"> <a href="' . get_category_link( $fcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . '" ' . '>View all posts in ' . $fcategory->name.'</a></dd>';
                    echo '</dl>';
                endforeach; 
                    }
                ?>

Every think seem to work perfectly except.... its showing only one post for all category while it should have shown one post from each category to be more specific its showing like

cat-1 cat-2 cat-3 cat-4 (this is ok)
post-1 post-1 post-1 post-1 (this is the prob)

while it should have been

cat-1 cat-2 cat-3 cat-4
post-1 post-2 post-3 post-4

Please help

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I've checked this code localy, here is the working snippet:

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

        $fcategories =   get_categories($cat_args); 

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

            $post_args = array(
                    'posts_per_page' => 1,
                    'cat' => $fcategory->cat_ID
                    );

            $fposts = query_posts($post_args);

            while(have_posts()) : the_post();    ?>
                <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
                <?php

                echo '<dd class="view-all"> <a href="' . get_category_link( $fcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . '" ' . '>View all posts in ' . $fcategory->name.'</a></dd>';
            echo '</dl>';
            endwhile; 
        }
        wp_query_reset();
?>

Original answer:
I think the problem might be here:

$post_args = array(
  'showposts' => 1,
  'category' => $fcategory->cat_ID
);

you're using category argument, it should be cat so try this code:

$post_args = array(
  'showposts' => 1,
  'cat' => $fcategory->cat_ID
);
share|improve this answer
    
no, 'category' is correct for get_post, as per codex –  user10105 Dec 24 '11 at 13:11
    
but have you tried using cat ? It might be a misstake in a get_posts() codex page because almost every other wordpress function (which is based on WP_Query) uses cat argument. You can also try using query_posts() instead of get_posts() –  mbajur Dec 24 '11 at 13:16
    
i have tried 'cat' but its not working, query_posts will replace the main query on my front page, this is the second query for featured section –  user10105 Dec 24 '11 at 13:19
    
yes, that's true, that's why while using query_posts, you can add wp_reset_query() after your custom loop to avoid such problem. That's how i was allways doing custom loops, query_posts() + wp_query_reset() and it allways works :) –  mbajur Dec 24 '11 at 13:21
    
it did not work for me :( even after reset_qery –  user10105 Dec 24 '11 at 13:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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