Sunday, June 19, 2011

Get post by category

Sometime we need get post by category in wordpress. Then just create a functions in functions.php in your theme folder with this line..

function get_post_by_category($category_id) {
    global $post;
    $args = array( 'numberposts' => 1, 'category' => $category_id );
    $myposts = get_posts( $args );
    return $myposts;

And use this code where you want to show your post with categories id.

<?php echo get_post_by_category($id); ?>

Instead of $id we use category id. That's all

No comments:

Post a Comment