WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Patrick Orr
    Member
    Posted 3 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:

    <?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; ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    $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

  3. Patrick Orr
    Member
    Posted 3 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
    Member
    Posted 3 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. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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)

Topic Closed

This topic has been closed to new replies.

About this Topic