WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How Do I Only Select One Post From One Category? (3 posts)

  1. technguide
    Member
    Posted 1 year ago #

    Let's say i want to get the latest post from 'liveblog' category, which part do i edit?

    <?php require('../wp-blog-header.php');?>
    <html>
        <?php
            $args = array( 'numberposts' => 1,
                            'post_status'=>"publish",
                            'post_type'=>"post",
                            'orderby'=>"post_date",
                            'category'=>"liveblog");
            $postslist = get_posts( $args );
    
            foreach ($postslist as $post) : setup_postdata($post);
            ?>
        Redirecting to <b>
        <?php
            echo the_permalink();
            echo "</b>. ";
            ?>
        If redirection doesn't take place within 5 seconds, click
        <a href = "<?php the_permalink()?>">here</a>.
        <br/><br/>
        <b>Post Name</b> : <?php the_title();?><br/>
        <b>Date Posted</b> : <?php the_date();?><br/>
        <script>
            //location = "<?php the_permalink()?>";
        </script>
        <?php endforeach; ?>
    
    </html>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    First, I believe that 'posts_per_page' is preferred over 'numberposts'.

    Second the parameter for the category should probably be 'category_name' instead of just 'category'. But, use the slug even though the parameter is category_name!

  3. technguide
    Member
    Posted 1 year ago #

    Awesome! I've edited it to be like this

    $args = array( 'posts_per_page' => 1,
                            'post_status'=>"publish",
                            'post_type'=>"post",
                            'orderby'=>"post_date",
                            'category_name'=>"liveblog-apple-keynote");

    and it worked flawlessly. Thank you once again!

Topic Closed

This topic has been closed to new replies.

About this Topic