Hari krishnan
Hari krishnan

Reputation: 2108

Displaying posts of a category wordpress

I am developing a wordpress theme from scratch. I have completed the design and am stuck at the listing of posts.

I am currently using:

$args = array( 'posts_per_page' => 10 );
$myposts = get_posts( $args );

It does display all the posts.

How can i display the posts in a category which is clicked?

I have no clue about how to achieve this. I tried to get the category name from URL and the pass it to the get_posts(). But i dont think this will be efficient because using different URL rewrite the URL can be changed.

Upvotes: 0

Views: 48

Answers (1)

LTasty
LTasty

Reputation: 2008

If you write your code in archive.php wordpress understand is a category and automatically return related posts otherwise by code you need to pass category id.

Example:

$args = array( 'posts_per_page' => 10,'cat'=>1 );
$myposts = get_posts( $args );

Upvotes: 1

Related Questions