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="https://wordpress.stackexchange.com/questions/166945/<?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