0

I want to display the titles of the last N posts in a specific category. I am using the following code:

        <?php
        $cat_id=get_cat_ID('category1');
            $posts = get_posts ("cat=$cat_id&posts_per_page=3");
            if ($posts) 
        {
        foreach ($posts as $post):
            setup_postdata($post); ?>
            <h2><?php
            //the_title(); 
            ?>
            </h2>
            <?php next_post_link( '%link', 'Next post in category', TRUE ); ?>
            <?php //the_content();
            ?> 
            <?php endforeach;
        }
            ?>  

But, instead of getting titles of the last 3 posts, I get TWO (instead of three) links, and even worse, the links returned are not post titles. They simply read "Next post in category". However, their href works well (I can open post content by clicking on them). How should I modify the code so that I get titles to last N posts with category "category1", where I can open the post content (in new page) when I click the link?

3 Answers 3

2

First, there is no parameter named cat in get_posts. Use category instead to input comma separated category IDs or category_name to directly inserting category name.

Second, in your code you have commented out the title of the post. Try this code.

<?php
    $cat_id = get_cat_ID('category1');
    $posts = get_posts( "category=$cat_id&posts_per_page=3" );
    if ($posts) {
        foreach ($posts as $post): setup_postdata($post); ?>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php endforeach;
    }
?>

The reason this shows two post could be because your category have 2 posts only. Make sure your category have atleast 3 posts.

You can change posts_per_page=3 in above code to whatever number of posts you want to list. If you want to list 10 posts, change it to posts_per_page=10 and if you want to list all posts then use -1 like this posts_per_page=-1

3
  • No my category has four posts. I am sure. Oct 30, 2014 at 14:05
  • I already used your code. It just lists titles in this category. I want titles shown as links to relevant post contents. Also putting <?php the_title();?> inside an anchor tag does not help as I don't know how to set it href. Oct 30, 2014 at 14:10
  • I updated answer with suggested change. Now post title links to posts. Use updated code.
    – Robert hue
    Oct 30, 2014 at 14:17
0

You can use this function in your theme. Add this function to your theme's functions.php and call function. This function gives you latest posts from category.

function latest_from_category( $post_count, $category) {
    $cid = get_cat_ID($category);
    $last_by_cat = new WP_Query('orderby=date&cat='.$cid.'&posts_per_page='.$post_count);
    echo '<h4>Latest Post From Category</h4> <ul>';
    while ($last_by_cat->have_posts()) : $last_by_cat->the_post();
?>
    <li><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;
echo '</ul>';
}

Usage of this function.

<?php latest_from_category('5','WordPress');?>

The number defines count of links and WordPress is a category name.

0

@Rebert Hue your code works fine. However, before checking out your edited answer, I modified my code as follows, which also sets href correctly:

<?php
                $cat_id=get_cat_ID('mycategory');
                    $posts = get_posts ("cat=$cat_id&posts_per_page=3");
                    if ($posts) 
                {
                foreach ($posts as $post):
                    setup_postdata($post); ?>
               <h2>
                    <a href="<?php _a(get_permalink());">
                           <?php the_title()?>

                    </a>
               </h2>

                    <?php endforeach;
                }
                    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.