SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru SG1's Avatar
    Join Date
    Jul 2005
    Location
    Virginia, USA.
    Posts
    877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code for listing posts for a category

    Anybody know how to lists, say the 5 most recent entries for a particular category ? I tried looknig through the codex but couldn't find the code.


  2. #2
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://codex.wordpress.org/Function_Reference/get_posts
    use the arguments to get posts from a specific category.
    Dan G
    Marketing Strategist & Consultant

  3. #3
    SitePoint Guru SG1's Avatar
    Join Date
    Jul 2005
    Location
    Virginia, USA.
    Posts
    877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    am i doing something wrong here. I have this code but it doesn't work. i'm trying to show the lasts 5 posts from the category id of 3

    Code PHP:
     <?php get_posts('category=3&numberposts=5'); ?>


  4. #4
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doesn't look like you checked out the example on that page, otherwise you'd see your error immediately. get_post builds an array of those posts, so you need to populate an array variable, and then use foreach to loop through - but that's all explained on the page I linked to.
    Dan G
    Marketing Strategist & Consultant

  5. #5
    SitePoint Guru SG1's Avatar
    Join Date
    Jul 2005
    Location
    Virginia, USA.
    Posts
    877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or maybe i did but just am a complete php newb that some of this is over my head.

    i will look again and try to decipher.


  6. #6
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SG1 View Post
    or maybe i did but just am a complete php newb that some of this is over my head.

    i will look again and try to decipher.
    should've said that you don't know much about php - in which case it'd be a good idea to learn some of the php basics first - variables, arrays, functions - otherwise it'd be like you'd go to China tomorrow without knowing one word and nothing about its culture.
    Dan G
    Marketing Strategist & Consultant

  7. #7
    SitePoint Guru SG1's Avatar
    Join Date
    Jul 2005
    Location
    Virginia, USA.
    Posts
    877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gosh thanks. i'll have to remember that one before i waste your precious time again. geez....

    everything i was told about wordpress was that it was easy to learn and didn't have to know php.


  8. #8
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what's your problem? yes, you don't need php for Wordpress - unless you start customising it. and seriously, learning the php basics isn't all that difficult.
    Dan G
    Marketing Strategist & Consultant

  9. #9
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this is your front page, then you can set that in the settings without having to deal with the code - sets how many posts show up on the first page.

    If you are setting it for something else, let us know

  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Keep it civil guys. Here's an example of showing the 5 most recent posts for a category:

    PHP Code:
    <?php
    global $post;
    $myposts get_posts('category_name=Featured');
    foreach(
    $myposts as $post) :
        
    setup_postdata($post);
    ?>
    <div class="post">
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <?php the_content(); ?>
    </div>
    <?php endforeach; ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •