Need a code to show posts of a category on wordpress sidebar

Discussion in 'Programming' started by rymdoll, Jul 31, 2008.

  1. #1
    Hello
    i need a code to display latest posts of a specific category on the sidebar of a wordpress blog.
    please help :)
     
    rymdoll, Jul 31, 2008 IP
  2. JSellnau_TSCC

    JSellnau_TSCC Peon

    Messages:
    166
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I have seen several widgets like this that will show post from all categories. I am sure you could tweak the code to show just one category.
     
    JSellnau_TSCC, Jul 31, 2008 IP
  3. rymdoll

    rymdoll Banned

    Messages:
    182
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i tried many widgets, none worked
     
    rymdoll, Jul 31, 2008 IP
  4. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have you tried this?
    James Lao's Category Posts Widget

    I don't know of one template tag that will do that, so you will probably need to add a block of code to the sidebar, like this:
    
    <ul>
       <?php
          $catposts = get_posts('numberposts=NUMPOSTS&category_name=CATEGORY');
          foreach($catposts as $post) :
       ?>
       <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       <?php endforeach; ?>
    </ul> 
    
    Code (markup):
    Change NUMPOSTS to the number of posts to list (eg. numberposts=5). It will get the latest posts by default.

    Change CATEGORY to the category stub text (eg. category_name=wordpress). If you want to use a category number, change category_name to category (eg. category=2)
     
    Cash Nebula, Aug 1, 2008 IP
  5. HobbesPDX

    HobbesPDX Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    THANK YOU.
    Even if the original writer never responds -- this is EXACTLY what I needed.
    I, too, had tried widgets, and they weren't working in my particular scenario.

    This is perfect.
    Thanks Cash Nebula.
     
    HobbesPDX, Sep 26, 2008 IP
  6. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hey, you're welcome :)
    I'm glad someone finally found it useful.
     
    Cash Nebula, Sep 27, 2008 IP
  7. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #7
    Now there is an exciting piece of code. I did not know that function existed and queried the database the "old fashioned way" [direct php/sql] to get the same results. Thanks.

    Q...
     
    QiSoftware, Sep 27, 2008 IP
  8. ysribharath

    ysribharath Well-Known Member

    Messages:
    745
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    155
    #8
    excellent ....its working awesome..Thanks for ur contribution
     
    ysribharath, Sep 17, 2009 IP
  9. orionindia

    orionindia Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The category number function is not working for me. Any guesses
     
    orionindia, Nov 13, 2009 IP
  10. ysribharath

    ysribharath Well-Known Member

    Messages:
    745
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    155
    #10
    Try this

    in the above code 15 is the number of posts and 6 are the category numbers .

    Replace it with ur own and if u want to include multiple categories in the side bar just add cat num followed by comma ( for ex : 6,7,8)
     
    ysribharath, Nov 21, 2009 IP
  11. DailyMorningCoffee

    DailyMorningCoffee Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Working great...thx for the code
     
    DailyMorningCoffee, May 24, 2011 IP