WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying excerpts from the latest posts in 3 categories on 1 page (7 posts)

  1. sumedh07
    Member
    Posted 6 years ago #

    I had a very cluttered up blog, and after a great deal of toil to make it more eclectic and organized, I am faced with a very perturbing problem. I need the help, therefore, of a wordpress techie genius in this regard:

    I have just three categories in my blog now, and the posts in all of them are mutually exclusive. Now, what I want, is to create a page which displays the excerpt of the latest posts in all the three categories (i.e. 3 excerpts- one from each category). So that I can use this 'static' page as my frontpage.

    I would be tremendously obliged if you help me and tell me how to get this done. Please bear in mind, though, that I am just copy-paste good when it comes to php programming.

  2. Kafkaesqui

    Posted 6 years ago #

    The best method here is to create a 'post loop' for each category. A simple framework for what you want would look like this:

    <?php query_posts('cat=1&showposts=1'); ?>
    <h2><?php echo get_the_category_by_id(1); ?></h2>
    <?php while (have_posts()) : the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    
    <?php query_posts('cat=2&showposts=1'); ?>
    <h2><?php echo get_the_category_by_id(2); ?></h2>
    <?php while (have_posts()) : the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    
    <?php query_posts('cat=3&showposts=1'); ?>
    <h2><?php echo get_the_category_by_id(3); ?></h2>
    <?php while (have_posts()) : the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>

    The only "PHP programming" you would need do is change (for each query) the category ID #s assigned to 'cat=' in query_posts() and the parameter for get_the_category_by_id().

    Note that in most cases I would use something besides query_posts here (see the first link below for examples), but as long as you are not doing anything else too complicated on your 'front' page, it shouldn't cause any issues.

    References:
    http://codex.wordpress.org/The_Loop#Multiple_Loops
    http://codex.wordpress.org/Template_Tags/query_posts

  3. sumedh07
    Member
    Posted 6 years ago #

    Thanks a lot, Kafkaesqui; but where exactly does this code go? Do I paste it in the "code" section of the Page Editor? Or do I have to create some sort of a theme-file for this?

  4. sumedh07
    Member
    Posted 6 years ago #

    The issue is resolved, thanks!
    I just pasted the above code and made a template for a page, which I then used for the front 'static' page. Works wonderfully here, thanks!

  5. mazen
    Member
    Posted 6 years ago #

    This post is most helpful indeed, saved me plugin headaches, thanks a lot Kafkaesqui.

    One question though, the loop as I see it assumes that it will find the posts, however what if the post was not found? I am using this as my homepage, if a wrong address was inserted people will get the 404 page, however, when the search is used, it simply gives back the homepage again :/

    It used to work in my old theme, and the difference is that when you have on IF loop <?php if (have_posts()) : ?> you can use <?php else : ?>, which used to redirect to the 404 page and the search again.

    Please check my current site: http://www.mazenville.com

    And let me know if there is a way to make the search work again, currently it is just useless, if a post is found or not, it just returns the homepage again, I am sure there is something I need to add to my index page.

    Cheers.

  6. Kafkaesqui

    Posted 6 years ago #

    "however, when the search is used"

    Simplest solution to your problem is to set up a home.php in your theme for these category loops on the home page, and leave the index.php as it originally was so it will function normally for any query types using it as the *default* template.

    See:
    http://codex.wordpress.org/Template_Hierarchy

  7. mazen
    Member
    Posted 6 years ago #

    Thanks Kafkaesqui,

    Works like charm.

    I should have realized this myself.

    Cheers.

Topic Closed

This topic has been closed to new replies.

About this Topic