0

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

1

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.

0

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');
  • Please format your answer. Please check this for help – Maruti Mohanty Dec 26 '13 at 6:35

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.