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.

This should be very easy, however for some reason I can't find a working snippet anywhere on the web.. I basically need to display one latest post from 6 categories (can be changed to 7,8, etc etc) using one loop. this should be the output: (just a demonstration)

<div class=box>
  <h2>name of category 1</h2>
  <a href="">The title and the permalink of the latest one post from category 1</a>
</div>

<div class=box>
  <h2>name of category 2</h2>
  <a href="">The title and the permalink of the latest one post from category 2</a>
</div>

<div class=box>
  <h2>name of category 3</h2>
  <a href="">The title and the permalink of the latest one post from category 3</a>
</div>

For now I am using 6 loops using simple query:

<?php query_posts('showposts=1&cat=8'); ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    //Title, links etc comes her....
<?php endwhile; ?>

But that makes me create 6 loops and I would like to do it easier using one loop and a possibility to control how many and which catefories I want to display.

Thanks A lot

share|improve this question
1  
    
OK that did the trick: wordpress.stackexchange.com/questions/35441/… the second link you posted is too complex and wasn't straight forward to me. Thanks –  gil hamer Aug 26 '12 at 11:36
    
well yeah it is complex... b/c to actually do this w/ a single query IS complex. i don't understand SQL either. the first method (multiple get_posts) is much more intuitive. though i would advise you to use the transient API to help minimize the cost of so many queries. cheers –  helgatheviking Aug 26 '12 at 11:57
add comment

2 Answers

What about in_category() function? You can build a conditional for those categories and then use a $var to show just one post, filtering previously those cats on the query. It would be something like that:

<?php query_posts( 'posts_per_page=-1&cat=1,2,3,4,5,6' ); $var = 0; ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <?php if ( in_category(1) && $var == 0 ) : ?>

        //Title, links etc comes here....
        <?php $var++; ?>

    <?php elseif ( in_category(2) && $var == 1 ): ?>

        //Title, links etc comes here....
        <?php $var++; ?>

    <?php elseif ( in_category(3) && $var == 2 ): ?>

        //Title, links etc comes here....
        <?php $var++; ?>

    <?php elseif ( in_category(4) && $var == 3 ): ?>

        //Title, links etc comes here....
        <?php $var++; ?>

    <?php elseif ( in_category(5) && $var == 4 ): ?>

        //Title, links etc comes here....
        <?php $var++; ?>

    <?php elseif ( in_category(6) && $var == 5 ): ?>

        //Title, links etc comes here....
        <?php $var++; ?>

    <?php endif; ?>

<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

You need to call all the post with posts_per_page=-1, but I still think that 1 query is better than 6. I know that is not the best option, but it works... I'm working to get a simpler code. Hope it helps.

share|improve this answer
add comment

My Answer : Here i want recent post from three categories Business Data,Real Estate,Investing Ans is like this

$realestate_args = array( 
'showposts' => '1', 
'category_name' => 'Business Data,Real Estate,Investing', 
'post_type' => 'post', 
'post_status' => 'publish', 
'orderby' => 'post_date', 
'order' => 'DESC');

Else if you have cat id's Ans:

$realestate_args = array( 
'showposts' => '1', 
'cat' => '112,113,115', 
'post_type' => 'post', 
'post_status' => 'publish', 
'orderby' => 'post_date', 
'order' => 'DESC');
share|improve this answer
    
Please format your answer. Please check this for help –  Maruti Mohanty Dec 26 '13 at 6:35
add comment

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.