• Resolved ClaudiaM

    (@claudiam)


    Hello,

    I will try to explain this as clearly as possible… Any help would be muchly appreciated =)

    I have four main categories in my site:
    1 – Updates
    2 – Tutorials
    3 – Graphics
    4 – Stock

    I wanted my index page to show the latest post from each category, like this:

    Latest Update Title
    Excerpt

    Latest Tutorial
    Title
    Excerpt

    Latest Graphic
    Image
    Name

    Latest Stock
    Image
    Name

    In order to do this, I have inserted four instances of the loop. For example, to show the latest update I have used something like:

    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category(1) ) { ?>
    <?php the_excerpt() ?>
    <?php } else {
    }; ?>
    <?php endwhile; ?>

    Then, I used the code query_posts($query_string . "showposts=4"); to make it display only 4 entries. Obviously this does not work as intended: if I have two posts from the category ‘Stock’ that have been created after the latest post from the category ‘Updates’, they will both show up. So, the place where the latest update was supposed to be will be left blank.

    The question is how can I show only the latest post by category, even if one of the categories is updated more frequently? As I am using 4 loops, can I instruct each of them to display only one post?

    Thank you so much in advance! 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • Have you tried breaking the loop?

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if ( in_category(1) ) { ?>
    <?php the_excerpt() ?>
    <?php break; ?>
    <?php } ?>

    Thread Starter ClaudiaM

    (@claudiam)

    I’ve tried it now, but it didn’t work 🙁

    Disclaimer: I know nothing about php. All my coding experience in is PERL and LPC. Maybe something like this? Or a similar tweak.

    <?php

    if ( have_posts() ) : while ( have_posts() ) : the_post() {

    a=0;
    b=0;
    c=0;
    d=0;
    if (a && b && c && d) break;
    switch (in_category()) {
    case 1:
    if(!a) {
    <!—category 1 stuff–>
    a++;
    } else { continue; }
    case 2:
    if(!b) {
    <!—category 2 stuff–>
    b++;
    } else { continue; }
    case 3:
    if(!c) {
    <!—category 3 stuff–>
    c++;
    } else { continue; }
    case 4:
    if(!d) {
    <!—category 4 stuff–>
    d++;
    } else { continue; }
    default: continue;
    }

    }
    ?>

    the cases should be the actual category number.

    You may need to include the number of posts allowed to show for something like this to work.

    Also, in what way didn’t the previous code work?

    Thread Starter ClaudiaM

    (@claudiam)

    I think I couldn’t use that because there are other html elements between each post, and I can’t put them inside The Loop… and I’m not handy enough in php to create my own tweak. But thank you, anyway!

    As for the other code, I added it where you told, but it didn’t affect anything on the page. The latest update is still a blank space and I have two posts from the category ‘stock’, when there was supposed to be only one.

    Anyway, I was thinking of something simple, like a query you can add to the beggining of the loop, like:

    <?php while ($onepost->have_posts()) : $onepost->the_post(); ?>

    And then define a custom query that would make it display only one post, like:

    $onepost = new WP_Query('showposts=1');

    But this doesn’t work either =P

    <?php query_posts(‘cat=1&showposts=1’); ?>
    <?php while (have_posts()) : the_post(); ?>
    <!– Do special_cat stuff… –>
    <?php endwhile;?>

    That’s from the “The Loop” in the Codex (http://codex.wordpress.org/The_Loop) does that help?

    Thread Starter ClaudiaM

    (@claudiam)

    Lol… I had read that before, but I forgot to try it because I thought I couldn’t use multiple query_posts tags. But anyway, I’ve tried and it worked! Thanks a lot! 🙂

    So, this is what I did to show the latest entry of each category on the index page (it will work if you do it in the normal Pages too):

    First, as the categories were 4 and I wanted the posts to be displayed differently (for example, three of the categories were inside a table and the other one not), I have inserted one loop for each of them. For example, to show category 1 I wrote:

    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category(1) ) { ?> // make it show only posts from the category nr. 1
    <?php the_title(); ?>
    <?php the_excerpt() ?>
    <?php } else {
    }; ?>
    <?php endwhile; ?>

    To make each of the loops display only one post, I added <?php query_posts('cat=CATID&showposts=1'); ?> before each of them. CATID is to be replaced by the id of the category. So, my end result was something like this:

    <!-- category 1 -->
    <?php query_posts('cat=1&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category(1) ) { ?> // make it show only posts from the category nr. 1
    <?php the_title(); ?>
    <?php the_excerpt() ?>
    <?php } else {
    }; ?>
    <?php endwhile; ?>
    <!-- category 2 -->
    <?php query_posts('cat=2&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category(2) ) { ?> // make it show only posts from the category nr. 1
    <?php the_title(); ?>
    <?php the_excerpt() ?>
    <?php } else {
    }; ?>
    <?php endwhile; ?>
    <!-- category 3 -->
    <?php query_posts('cat=3&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category(3) ) { ?> // make it show only posts from the category nr. 1
    <?php the_title(); ?>
    <?php the_excerpt() ?>
    <?php } else {
    }; ?>
    <?php endwhile; ?>
    <!-- category 4 -->
    <?php query_posts('cat=4&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category(4) ) { ?> // make it show only posts from the category nr. 1
    <?php the_title(); ?>
    <?php the_excerpt() ?>
    <?php } else {
    }; ?>
    <?php endwhile; ?>

    If one of the categories has child categories that you want to include in the loop, the query strings remain the same (as they include the child categories too) but you have to add the numbers of these categories to the in_category condition. For example, if the categories 10 and 11 were sub-categories of 4:

    if ( in_category(4) || in_category(10) || in_category(11) )

    Well, that’s it!

    Thanks so much, this almost works for me, saylorville.org is my site, but as for now it’s not working completly, I just dropped this in right before “the loop”, do I need to take the loop completly out? how will this effect my other post or pages? Thanks!

    This is your new Loop. Actually, Loops 🙂
    Your should replace the existing one with the code above in your index.php template file.

    Whether other views will be affected that depends on the structure of your theme, see Template Hierarchy in the Codex:
    http://codex.wordpress.org/Template_Hierarchy

    I´m trying to use this code

    <?php query_posts('cat=4&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category(4) ) { ?> // make it show only posts from the category nr. 1
    <?php the_title(); ?>
    <?php the_excerpt() ?>
    <?php } else {
    }; ?>
    <?php endwhile; ?>

    to display only one post pr category by using a template pr category. The result I’m hoping for is that my front page will display links to pages that only show the latest in the different categories.

    This code ClaudiaM posted above works, but I want to display more that just the title and the_excerpt. I want the page to look just like a full post view. I copied the code from the single.php file, and it works. kinda. The comments dosn’t show. The <?php comments_template(); ?> doesn’t do anything. How come?

    agentd

    (@agentd)

    I am also trying to do exactly the same thing. Eiriks, did it work at the end? How did you solve it? Please help!

    Many thanks.

    to show the comments try this:
    <?php include (TEMPLATEPATH . ‘/comments.php’); ?>

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Show one post by category on the index page’ is closed to new replies.