user1957109
user1957109

Reputation:

Wordpress + get post by category

I am new to wordpress so may be this is a very obvious question.

How can i get posts of a specific category only.

Upvotes: 0

Views: 4201

Answers (5)

Vimal Saifudin
Vimal Saifudin

Reputation: 1845

    <?php 
          $args = array( 
           'posts_per_page' => -1, 
           'offset'=> 1, 
           'category' => 5, 
           'orderby' => 'ID',
           'order' => 'ASC'
          );
          $posts = get_posts($args);
          foreach ( $posts as $post ) : setup_postdata( $post ); ?>
               the_title(); 
               the_excerpt(); 
               the_permalink(); 
               the_content(); 
          endforeach; 
          wp_reset_postdata();
    ?>

Upvotes: 0

Gaurab paul
Gaurab paul

Reputation: 9

<?php
    //here 1 is category id
    $args = array( 'cat' => 1);
    query_posts( $args );
    while ( have_posts() ) : the_post();
        the_title();
        the_content();
    endwhile;
    wp_reset_query();
?>

Upvotes: 0

Klevis Miho
Klevis Miho

Reputation: 859

I would encourage you to use WP_Query:

<?php
    // The Query
    $the_query = new WP_Query( 
        'category_name' => 'slug-of-category',
    );

    // The Loop
    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    endwhile;

    // Restore original Query & Post Data
    wp_reset_query();
    wp_reset_postdata();
?>

Upvotes: 0

lumos
lumos

Reputation: 753

You can also try following other methods

<?php
    $post = query_posts( array ( 'category_name' => 'uncategorized') );
    $post = query_posts( array ( 'category_slug' => 'uncategorized') );
    $post = query_posts( array ( 'category' => 1) );
?>

Upvotes: 3

William Buttlicker
William Buttlicker

Reputation: 6000

Try this:

 <?php
   $posts = get_posts(array('category' => 1));
 ?>

Upvotes: 0

Related Questions