Guna
Guna

Reputation: 15

How to get all posts from one category in Wordpress

How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.

  include('wp-config.php');


  global $wp_query;

 $args = ('category=news&posts_per_page=-1');

 $myposts = get_posts( $args );


 foreach ( $myposts as $post ) : setup_postdata( $post ); 
   $result =  array(
   "id"=>$args['ID'],
   "type"=>$args['post_type'],
   "url"=>$args['guid']);
 endforeach; 

wp_reset_postdata();
print($result);

Upvotes: 0

Views: 2608

Answers (4)

tempranova
tempranova

Reputation: 937

are you just trying to get posts from a category?

This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.

query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;

This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.

http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category

AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.

Upvotes: 1

Anoop Asok
Anoop Asok

Reputation: 1337

If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.

<?php
    if(have_posts()) :
        while (have_posts()) : the_post();
?>
            <a href="<?php the_permalink();?>"><?php the_title();?></a>
            <?php
                the_post_thumbnail();
                the_content();    
            ?>
<?php
        endwhile;        
    endif;
?>

If you want to display the same in pages other than category page, just add the following code to the corresponding files.

<?php
    $posts = new WP_Query();
    $posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
    if($posts->have_posts()) :
        while ($posts->have_posts()) : $posts->the_post();
?>
            <a href="<?php the_permalink();?>"><?php the_title();?></a>
            <?php
                the_post_thumbnail();
                the_content();    
            ?>
<?php
        endwhile;        
    endif;
    wp_reset_postdata();
?>

Upvotes: 1

Khushboo
Khushboo

Reputation: 1817

Try below :-

    global $wp_query;

 $args = ('category=news&posts_per_page=-1');

 $myposts = get_posts( $args );


 foreach ( $myposts as $post ) : setup_postdata( $post ); 

   $result[] =  array(
   "id"=>$post->ID, // changed $args to $post
   "type"=>$post->post_type,
   "url"=>$post->guid);
 endforeach; 

wp_reset_postdata();
print_r($result);

Upvotes: 2

Ananta Zenda
Ananta Zenda

Reputation: 1

On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:

<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink();?>"><?php the_title();?></a>
    <?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>

Upvotes: 0

Related Questions