Wordpress displays posts of category ID

46,351

Solution 1

try below code:

<?php
$current_cat_id  = get_query_var('cat');
$showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post(); ?>      
    <div class="timeline">
    <h3><?php the_title(); ?></h3>
    <?php the_content();?>
    <?php endwhile; else: ?>
    <?php _e('No Posts Sorry.'); ?>
    <?php endif; ?>
</div>

Solution 2

If you are using category.php, you can omit the query_posts and it will auto fill in the posts for you.

Solution 3

    <?php
    // Start the loop.
    $categories = get_categories('child_of=1');//Parents category id
                        foreach ($categories as $cat) {

                            $option = '<a href="/category/archives/'.$cat->category_nicename.'">';
                            $option .= $cat->cat_name;//parents sub category name
                            $option .= '</a>';
                            echo $option;
                            query_posts('cat=$cat->cat_ID&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>      

<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif;  }?>
Share:
46,351
Miker
Author by

Miker

Updated on July 07, 2022

Comments

  • Miker
    Miker almost 2 years

    Can't seem to find the right answer for what I thought would be trivial.

    I have some categories arranged like this...

    Parent Category 1
    - Child Category 1
    - Child Category 2
    - Child Category 3

    ...and I have some posts that are in Child Category 2. I want my page to display all posts from the category I am currently in.

    This is what I am doing right now:

    <?php
    query_posts('cat=2&showposts=10');
        if (have_posts()) : while (have_posts()) : the_post(); ?>      
        <div class="timeline">
        <h3><?php the_title(); ?></h3>
        <?php the_content();?>
        <?php endwhile; else: ?>
        <?php _e('No Posts Sorry.'); ?>
        <?php endif; ?>
    </div>
    

    As you can see I am having to manually specify the category (cat=2), but instead I want it to automatically detect the category I am already in and display the posts (that way if I'm in a different category it will display those posts).

    Any suggestions?

    Thanks in advance. (SO Community = Awesome Sauce).

  • Miker
    Miker almost 12 years
    I think you have an error in that: 'posts_per_page'=> $showposts _no, should be this just be... 'posts_per_page'=> $showposts, Anyhoo, that shows all posts instead of just the ones for that category :-( Also if I throw a echo $current_cat_id; in the loop it doesn't display the category ID as expected, so I can only assume it's not actually getting it.
  • versvs
    versvs over 9 years
    Why aren't you using it? You say you want to "display all posts from the category I am currently in". Best practice when trying to achieve this is tu use the category.php (and child files), as described in Codex.
  • NightOwl
    NightOwl about 6 years
    Thanks sandip. You saved my time.