treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] 2 loops, 2 categories - can I do this?

  • Here's what I would like to do:

    On the left hand side of my home page to have a list of my 5 latest posts that would have the category of Blog and in the centre to have a list of my 3 latest posts with the category of Portfolio. Both sections would be styled differently and would take visitors to two separate sections of the site - Blog and Porfolio - which would look very different from one another.

    I have had a look around the web for this but most of the searches that show for multi loops seem to deal with having a featured post displaying different from the posts below.

    Is what I am wanting to do possible? I am fairly new to Wordpress and my knowledge of PHP is very, very limited.

    Thanks for any help.

    Barry
  • You simply are going to have two loops on the page, as far as I know there is no problem with that.

    If you're comfortable doing one loop and one category, doing a second should be as easy as copy-paste.
  • Maybe my understanding of the loop is not very clear. I tried doing that but my page didn't show anything apart from some error message. Probably didn't specify category properly.

    An extra issue is that I am not wanting to use index.php as my home page, so I need to have a Blog index page and another index page for Portfolio.

    With my limited knowledge I may be biting off more than I can chew.
  • I did come across the Perishable Press one but was a bit daunted by it and wasn't sure it was what I needed.

    Will give it a go on a test site and see where I get with it.

    Thanks for your help.
  • look into the query_posts function. That's what you'll need to call before the loop to set the category and number of posts. Set it once for the first loop, run the loop, set it again, run the second loop.

  • <div id=\"Blog-posts\">
    <?php if (have_posts()) : ?>
    <?php query_posts('category_name=Blog&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>

    <div class=\"post\" id=\"post-<?php the_ID(); ?>\">
    <h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"><?php the_title(); ?></a></h2>
    <p class=\"post-entry-date\"><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p>

    <?php the_content('Continue reading... &raquo;'); ?>

    <p><?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
    </div>

    <?php endwhile; ?>

    <ul>
    <li><?php next_posts_link('&laquo; Older Entries') ?></li>
    <li><?php previous_posts_link('Newer Entries &raquo;') ?></li>
    </ul>

    <?php else : ?>

    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . \"/searchform.php\"); ?>

    <?php endif; ?>
    </div>
    <div id=\"portfolio-section\">
    <?php if (have_posts()) : ?>
    <?php query_posts('category_name=Portfolio&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>

    <div class=\"post\" id=\"post-<?php the_ID(); ?>\">
    <h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"><?php the_title(); ?></a></h2>
    <p class=\"post-entry-date\"><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p>

    <?php the_content('Continue reading... &raquo;'); ?>

    <p><?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
    </div>

    <?php endwhile; ?>

    <ul>
    <li><?php next_posts_link('&laquo; Older Entries') ?></li>
    <li><?php previous_posts_link('Newer Entries &raquo;') ?></li>
    </ul>

    <?php else : ?>

    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . \"/searchform.php\"); ?>

    <?php endif; ?>
    </div>


    Here is a typical double loop in action with query_posts as Chris mentioned, a modification of what I used on my last test blog.
    It creates two divs, where each holds the sections you talked about creating.
    The

    <?php query_posts('category_name=Blog&showposts=5'); ?>
    <?php query_posts('category_name=Portfolio&showposts=5'); ?>
    areas are where you insert your parameters.
  • Guys,

    Thanks for the help so far. I have made some progress on a (very badly designed) http://www.bazzasblog.co.uk.

    As you can see the home page displays two loops - one for Blog and the other for Portfolio. I have managed to edit the code on the index.php so it only shows up the bolg category posts. I did this by entering <?php query_posts("category_name=blog"); ?>.

    What I know need to do is create another index.php page with <?php query_posts("category_name=portfolio"); ?> inserted. Obviously this isn't going to be that simple as I can't create another page.

    What do I do? Can I create index2.php? If so, how do I point the Portfolio entries to that page? And how do I get these entries to show up on my Portfolio 'page'?

    Thanks again for your help. And Chris thanks for all your tutorials and screencasts - they have taught me so much.
  • All sorted. I saved the index.php as index2.php. Made it a template and set the category for Portfolio posts only.

    To my surprise it worked!

    Now on to custom fields for the Portfolio items.
  • "bazzablue" said:
    All sorted. I saved the index.php as index2.php. Made it a template and set the category for Portfolio posts only.

    You should try to give the file name a little more importance, something like "static-home.php" or "home-page.php", something like that. "index2.php" could be confusing later on, especially with index.php kicking around.
  • That is the proper way to do it, unless you are going to reuse that template page though, I'd suggest naming it something like portfolio.php to better keep track of things as you go. Index2.php is not very descriptive. Also, when you declare the template name, you should name it something cohesive live "Portfolio" or "portfolio-template"

    As far as the query_posts code though, I ran in to a situation where when the Page was not the index page, it would not respect the read more links, but just show the entire post. (You may experience this) I never bothered to fix it... Just a heads up as far as that goes.
  • Good call. This is only a testing site so I wasn't too worried about naming stuff helpfully.

    Thanks for your help again.