WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need to display specific posts from one category (5 posts)

  1. gero_007
    Member
    Posted 2 years ago #

    Hi there everyone. I am new at wordpress and hope anyone can help...
    I have one category (catID=12) with 10 posts in it. What I want to do is to print the 5 recent posts in the same page (let's say in my homepage), but in different positions of the page. What i am doing is:

    $category_number = '12';
    $same_cat_posts = get_posts("numberposts=5&category={$category_number}");

    This will print the 5 posts I need, but all of them in the same part of the website - my question: Is there anyway that helps me get, for example "Post one", "post two" etc. as different variables and after that print the post where I want? (maybe any kind of array or so on?)
    Thanx a lot in advance :)

  2. Peter_L
    Member
    Posted 2 years ago #

    $same_cat_posts is an array.

  3. gero_007
    Member
    Posted 2 years ago #

    Thanks for your reply Peter_L, i know it's an array and I can print it's values by the following code:

    <?php foreach($same_cat_posts as $post) :?>
    <a href="#"><a href="<?php the_permalink(); ?>">
    <?php the_title(); ?> </a>
    <?php endforeach; ?>

    But, how can i print the_content(); in this case? Any idea? It doesn't print anything when I try it...??It prints the the_permalink() and the_title() but there is NO OUTPUT for the_content() or the_excerpt()...
    Thanx again!

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Template_Tags/get_posts#Examples

    try and add setup_postdata($post); to your code; then you should be able to use the_content(); etc.

  5. gero_007
    Member
    Posted 2 years ago #

    thank you alchymyth, thanx a lot! This is how it's working now...

    $args = array( 'numberposts' => 1, 'order'=> 'ASC', 'orderby' => 'title', 'category' => 12 );
    $postslist = get_posts( $args );
    foreach ($postslist as $post) :  setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?> </a>
    <?php the_excerpt(); ?>
    <?php echo "Posted on: " ;
    the_date(); ?></p>'

Topic Closed

This topic has been closed to new replies.

About this Topic