Ready to get started?Download WordPress


using $myposts to fetch posts from a specific catagory (5 posts)

  1. Patrick Orr
    Posted 4 years ago #

    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:

             global $post;
             $myposts = get_posts('numberposts=5&offset=news&category=news');
             foreach($myposts as $post) :
             <div class="excerpt">
                 <div class="excerpttitle">
                    <?php the_title();?>
                 <div class="thumbnail">
                        <?php the_post_thumbnail( 'thumbnail' ); ?>
                 <div class="excerpttext">
                        <p><?php the_excerpt(); ?></p>
                 <span class="readmore"><a href="<?php the_permalink(); ?>">Read More</a></span>
             <?php endforeach; ?>
  2. $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
    (string) (optional) Only show posts from this category name or category slug.
    Default: None


  3. Patrick Orr
    Posted 4 years ago #

    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:)

  4. rakemyleaves
    Posted 4 years ago #

    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.

  5. you can use a category name - but you have to use:

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

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

Topic Closed

This topic has been closed to new replies.

About this Topic