Grow your CSS skills. Land your dream job.

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

  • # September 30, 2009 at 3:22 pm

    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

    # September 30, 2009 at 4:19 pm

    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.

    # September 30, 2009 at 4:29 pm

    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.

    # September 30, 2009 at 4:39 pm

    Here’s one post that I found:

    http://wordpressmodder.org/multiple-loo … .html#hide

    And here’s one with three loops:

    http://perishablepress.com/press/2006/1 … wordpress/

    # September 30, 2009 at 5:17 pm

    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.

    # September 30, 2009 at 10:34 pm

    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.

    # October 1, 2009 at 12:00 am
    Code:
    < ?php if (have_posts()) : ?>
    < ?php query_posts('category_name=Blog&showposts=5'); ?>
    < ?php while (have_posts()) : the_post(); ?>

    ” rel=”bookmark” title=”Permanent Link to < ?php the_title_attribute(); ?>“>< ?php the_title(); ?>

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

    < ?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>

    < ?php endwhile; ?>

    • < ?php next_posts_link('« Older Entries') ?>
    • < ?php previous_posts_link('Newer Entries »') ?>

    < ?php else : ?>

    Not Found

    Sorry, but you are looking for something that isn’t here.

    < ?php include (TEMPLATEPATH . "/searchform.php"); ?>

    < ?php endif; ?>

    < ?php if (have_posts()) : ?>
    < ?php query_posts('category_name=Portfolio&showposts=5'); ?>
    < ?php while (have_posts()) : the_post(); ?>

    ” rel=”bookmark” title=”Permanent Link to < ?php the_title_attribute(); ?>“>< ?php the_title(); ?>

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

    < ?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>

    < ?php endwhile; ?>

    • < ?php next_posts_link('« Older Entries') ?>
    • < ?php previous_posts_link('Newer Entries »') ?>

    < ?php else : ?>

    Not Found

    Sorry, but you are looking for something that isn’t here.

    < ?php include (TEMPLATEPATH . "/searchform.php"); ?>

    < ?php endif; ?>

    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

    Code:
    < ?php query_posts('category_name=Blog&showposts=5'); ?>
    < ?php query_posts('category_name=Portfolio&showposts=5'); ?>

    areas are where you insert your parameters.

    # October 1, 2009 at 7:46 am

    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.

    # October 1, 2009 at 11:55 am

    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.

    # October 1, 2009 at 12:01 pm
    "bazzablue" wrote:
    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.

    # October 1, 2009 at 12:07 pm

    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.

    # October 2, 2009 at 6:22 am

    Good call. This is only a testing site so I wasn’t too worried about naming stuff helpfully.

    Thanks for your help again.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".