• Resolved adam533

    (@adam533)


    Hello
    I have setup a few pages on my site and also setup separate page templates for each page. From there I was going to add in code to display posts on each page depending on the category id number.

    My question is what would be the appropriate code for the loop to display the posts from only a certain category id. I have been reading about it and came up with this:

    <?php
    $lastposts = get_posts('numberposts=10&category=6');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>

    Though I am not sure if that is correct. It does work but only the first two posts appear correctly, the next two go down to the bottom of the page and past the sidebar. I also set up a custom field and a css style for that fields data. It only appears correctly (the style) on the first post.

    You can see the results on my site (which I am just starting to design)
    My Site

    I appreciate any help anyone can offer. I am just starting on wordpress and have been working on this for about ten hours today. I am at the point now where I have to walk away because it is all getting confusing 🙂 It is probably an easy solution that I am not seeing.

    THANKS

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi

    This is relatively simple. You don’t need get_posts. Just put a conditional query_posts on your page.php before the loop starts

    Read this
    http://codex.wordpress.org/Template_Tags/query_posts

    Put code like this before the loop

    ?php if ((is_page(‘portfolio’)) {
    query_posts(‘cat=2’);
    } elseif ((is_page(‘services’)) {
    query_posts(‘cat=6’);
    } elseif ((is_home() {
    query_posts(‘cat=9’);
    } ?>
    —- Regular Loop Code goes here

    is_home() is your blog page

    in cat=, sub your catid for the #’s I inserted

    Thread Starter adam533

    (@adam533)

    Thanks for your help (on my other post also).

    I tried that method but was still getting errors – after looking closer I realized I had some of the other code on the page incorrect.

    It is all working great now and I am able to display certain posts on each page now which will make updating so much easier!

    Thanks for your help. I am starting to pick this up more now 🙂

    Adam

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to display posts from a certain category on a page’ is closed to new replies.