WordPress.org

Support

Support » How-To and Troubleshooting » using $myposts to fetch posts from a specific catagory

using $myposts to fetch posts from a specific catagory

  • Hey All,
    I’ve been trying to set up a page template that will include a sidebar displaying post information from a certain category.

    I can display the posts fine, but I cannot make it only use the categories(that I set when I publish the post).

    Here is what I have so far:

    <?php
             global $post;
             $myposts = get_posts('numberposts=5&offset=news&category=news');
             foreach($myposts as $post) :
               setup_postdata($post);
             ?>
    
             <div class="excerpt">
                 <div class="excerpttitle">
                    <?php the_title();?>
                 </div>
                 <div class="thumbnail">
                        <?php the_post_thumbnail( 'thumbnail' ); ?>
                 </div>
                 <div class="excerpttext">
                        <p><?php the_excerpt(); ?></p>
                 </div>
                 <span class="readmore"><a href="<?php the_permalink(); ?>">Read More</a></span>
             </div>
             <?php endforeach; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Michael

    @alchymyth

    Forum Moderator

    $category
    (integer) (optional) Only show posts from this category ID. Making the category ID negative (-3 rather than 3) will show results not matching that category ID. Multiple category IDs can be specified by separating the category IDs with commas – but an array of IDs does not work.
    Default: None
    $category_name
    (string) (optional) Only show posts from this category name or category slug.
    Default: None

    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Template_Tags/get_posts#Parameters:_WordPress_2.5_And_Older

    Thanks for posting the codex…unfortunately I have already read this which lead me to posting on the forum.

    I am wondering why category is supposed to be an integer when you specify anything to be the category name(in the WP user interface). Do I really need to name all my categories numerically?

    I am guessing that I am getting something confused between the category name and the category ID…. Anyone have an idea?

    Thanks in advance:)

    In Firefox, mouse over a category name/link and its ID will appear in the url on the bottom browser bar. Right click and copy url to see id in other browsers.

    *edit- in the admin interface, not on the actual site.

    Moderator Michael

    @alchymyth

    Forum Moderator

    you can use a category name – but you have to use:

    $category_name
    (string) (optional) Only show posts from this category name or category slug.
    Default: None

    (this was part of the quote in my last reply)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘using $myposts to fetch posts from a specific catagory’ is closed to new replies.