Grow your CSS skills. Land your dream job.

Getting posts from different categories on different pages

  • Bob
    # October 5, 2012 at 3:21 pm

    Hi all,

    I’m trying to accomplish something which I think has to do with multiple loops.

    I have a website and I want to have a news section on the homepage. So that would be the latest posts.

    Then, I have multiple pages on which I want to have posts from certain categories. For example, I have a ‘study’ page, a ‘sports’ page, and a ‘work’ page. I’d like to have posts about studying on the study page, about sports on the sports page and of course posts about work on the work page.

    My guess is to have a normal loop on the homepage, and have loops that only display posts from specific categories on the other pages. It wouldn’t matter that much if new posts from one of the pages would also be displayed on the home page.

    However, I have no idea how to do this. I’ve tried things, like specifying from which categories I want to get posts but nothing seems to work.

    Could anyone direct me in the right direction on how to accomplish this? I’d say it’s got to do with multiple loops, and I’ve read things about resetting the loop but I can’t get my head around it. Help would be much appreciated :)

    # October 5, 2012 at 3:30 pm

    You can list posts from a certain category just by specifying it in the URL, usually by doing this:

    http://www.mysite.com/study
    http://www.mysite.com/sports
    http://www.mysite.com/work

    Wouldn’t that be just as easy?

    Bob
    # October 5, 2012 at 4:33 pm

    Of course I have those pages, but now I need to display the posts belonging to that certain category on those pages.

    # October 5, 2012 at 5:13 pm

    Sorry, what I meant was these URLs:

    http://www.mysite.com/category/study
    http://www.mysite.com/category/sports
    http://www.mysite.com/category/work

    I believe that WordPress, by default, uses those URLs to show posts in the categories in question, but you can change that in SETTINGS – PERMALINKS.

    On my own site I don’t need the “/category” part in the URL but I forgot how I did that exactly, or if that was built in in the theme or something…. o_O

    Bob
    # October 7, 2012 at 3:06 pm

    Hmm.. could be an option, but I have a menu that is dynamically made by WordPress and I don’t know if (or how) it can include categories as menu items.

    Anyway, I figured it would be easier to create templates for each page… each template with a loop that only get posts from a certain category, and then create pages in the WordPress backend and assign the right template to it. If there’s a better way to do this, I’d like to hear also.

    If not, my original question remains.. so how would those loops look like?

    # October 7, 2012 at 4:56 pm

    `query_posts( array ( ‘category_name’ => ‘my-category-slug’, ‘posts_per_page’ => -1 ) );`

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

You must be logged in to reply to this topic.

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