Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On my category page (not single.php) I'd like to have the recent posts from that category in the sidebar, it also needs to be dynamic.

I'm already doing something similar on single.php, but I can't think how to do it on the category.php page.

    <div class="related">


    <?php


    $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );

    if( $related ) {

    echo '<h3>Related Articles</h3> <ul>';

    }
    if( $related ) foreach( $related as $post ) {
    setup_postdata($post); ?>




            <li>
            <span class="thetime">
               <?php the_time('F jS, Y') ?>
            </span>
            <?php the_post_thumbnail('sidethumb'); ?>


            <h4><a href="<?php the_permalink() ?>"><?php the_title (); ?></a></h4>

            <p><?php echo excerpt(25); ?></p>

            </li>


    <?php }

    wp_reset_postdata(); ?>
     </ul> 





    </div> <!-- end div related --> 

Is it possible to have say the 20 recent posts from on the currently selected category?

Uh, so I can't get your code to work.

                <?php
    $max_posts = 20;
    $cat_ID = get_cat_ID ( single_cat_title( '', false ) );
    $args = array(
        'category' => $cat_ID,
        'numberposts' => $max_posts,
    );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata( $post );
        // display your posts with the_title(), the_excerpt(), etc
      <php the_title (); ?> 

    }
?>

If I put the <php the_title (); ?> etc in where you've written display your posts with title etc, it gives me a blank page.

My category.php looks like this

<?php get_header(); ?>


<?php get_sidebar('catbar'); ?>



    <div class="main-content">

        <div class="center" id="scale1">
            <div id="content" class="longform">


        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <article <?php post_class('item') ?> id="post-<?php the_ID(); ?>">
                <div class="meta">
                    <div class="metastuff">
                        <div class="authorname">
                        <h4>By <a href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>"><?php the_author_meta('display_name'); ?></a></h4>
                        </div>

                                            </div>

                        <span class="popupcomments">

                                <?php comments_popup_link ('0', '1', '%'); ?>

                        </span> 

                </div> <!-- end div meta -->


                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <div class="authordate">
                        <span class="authordate-inner"><?php the_time('F jS, Y') ?></span>
                </div>





                <?php
                    if(has_post_thumbnail()) :?>
                    <div class="figure">

                        <?php the_post_thumbnail(''); ?>
                        <?php if( !empty( get_post( get_post_thumbnail_id() )->post_excerpt ) ) { ?>
                            <div class="postthumbnailtext">
                                <?php echo get_post( get_post_thumbnail_id() )->post_excerpt ?>
                            </div>
                        <?php } ?>
                    </div> <!-- end div figure //centers thumbnail -->
                <?php else :?>
                <?php endif;?>


                <div class="the-content-container">
                    <?php the_content('Read Full Article'); ?>
                </div>



                <div class="the-sharing-field"> 

                 <label>Share this Article:</label>
                 <input type="text" value="<?php the_permalink (); ?>">

                <span class="reveal-share-bar">Show Sharing Options</span>                      
                </div>  

                    <div class="sharing-mp sharing-mp-hidden">
                    <ul>

            <li>    
                <a  class="social-email" rel="nofollow" title="Email this page" href="mailto:?Subject=<?php the_title(); ?>&body=<?php the_permalink(); ?>"></a>    
            </li>
            <li>    
                <a class="social-twitter" rel="nofollow" href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Share page via Twitter" target="_blank"></a>
            </li>   
            <li>
                <a class="social-facebook" title="Share page via Facebook" rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" target="blank"></a>
            </li>
            <li>    
                <a class="social-goo" title="Share page via Google+"  href="http://plus.google.com/share?url=<?php the_permalink (); ?>" rel="nofollow" target="_blank"></a>
            </li>
            <li>    
                <a class="social-linked" title="Share page via LinkedIn" rel="nofollow" href="http://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink (); ?>" target="_blank"></a>
            </li>

        </ul>



                </div> <!-- end div sharing-mp -->



                    <div class="postbottom"></div>

                    <div class="clearfix"></div>

                </article> <!-- end div post -->



        <?php endwhile; ?>


        <?php endif; ?>

            </div> <!-- end div content -->

            <div id="nav-below" class="navigation">

                <?php if(function_exists('wp_pagenavi')) { ?>

                <?php wp_pagenavi(); ?>

                <?php } else { ?>  

                <div class="nav-previous"><?php next_posts_link(__('Load More Articles')) ?></div>

                <div class="nav-next"><?php previous_posts_link(__('Newer posts')) ?></div>

                <?php } ?>

            </div>  <!-- end div nav-below -->
        </div> <!-- end div center -->



        <div class="clearfix"></div>    


    </div> <!-- end div main-content -->




<?php get_footer(); ?>
share|improve this question
    
Is your code sample taken from your single.php template, or is it the code you propose for category.php? –  Pat J Apr 11 '13 at 16:14
    
@Pat J That's from my single.php, it just gets the last 5 posts from the same category(s) as the current post being viewed. What I'd like to do is have the last 20 posts in the same category on category.php, in the sidebar. –  andy Apr 12 '13 at 7:27
    
What does your category.php look like now? –  Pat J Apr 12 '13 at 16:05
add comment

1 Answer

up vote 0 down vote accepted

Here's how I'd get the 20 (at most) most recent posts in the category.php page:

<?php
    $max_posts = 20;
    $cat_ID = get_cat_ID ( single_cat_title( '', false ) );
    $args = array(
        'category' => $cat_ID,
        'numberposts' => $max_posts,
    );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata( $post );
    ?>
    <!-- add your HTML here, including the_title(), the_excerpt(), etc -->
    <?php

    }
?>

The code uses get_cat_ID() and single_cat_title() to get the ID of the current category. I've done some (very) minimal testing to make sure that it at least excludes posts that aren't in the current category.

share|improve this answer
    
Edited my first post, I can't get it to work. –  andy Apr 12 '13 at 23:57
    
I've edited my code too. Note that <php the_title(); ?> will not work; it needs to be <?php the_title(); ?> instead. That question mark is important. –  Pat J Apr 13 '13 at 0:53
    
Ah yes, I'm an idiot! That's excellent, Pat, thank you very much! –  andy Apr 13 '13 at 12:11
    
do I have to do endforeach; or anything to close it off? –  andy Apr 13 '13 at 17:59
    
No, the last } in the code closes off the foreach -- it's essentially foreach( $myposts as $post ) { }. –  Pat J Apr 13 '13 at 18:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.