In single.php list other articles in this category (4 posts)

  1. frankfolk
    Posted 4 years ago #

    Hello, i'm close to solving this problem but there is just something missing...

    Can someone please help. I am editing single.php and want a side bar that lists all other articles as links that are in the same category as this post. I would also like it to show the date that post was created.

    This is what i have:

    <?php foreach((get_the_category($post->ID)) as $category) { ?>
                	<a class ="postLink" href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
                    <?php the_date(); ?> <br/>
                <?php }; ?>

    Basically, this just behaves strangely. If there is only one post in the category it posts the name twice and only one date. So guessing my loop isn't working correctly.

    Anyone have and ideas?

  2. frankfolk
    Posted 4 years ago #

    This is what i have now. And it works. But still struggling to incorporate the date. It only does it for the first entry in the loop.

            	<?php foreach((get_the_category($post->ID)) as $category) { ?>
            		<?php $catVal = $category->cat_ID; }
              	 $IDOutsideLoop = $post->ID;
              	 global $post;
            $myposts = get_posts('category='.$catVal.'&numberposts=999&order=ASC');
               foreach ($myposts as $post) { ?>
                 <li<?php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>>
                 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                 <p> <?php the_date(); ?> </p>
            <?php }; ?>
  3. Michael
    Forum Moderator
    Posted 4 years ago #

    use setup_postdata(); in the loop - http://codex.wordpress.org/Template_Tags/get_posts#Examples

    and be aware of the behaviour of the_date() - see SPECIAL NOTE in http://codex.wordpress.org/Function_Reference/the_date

  4. frankfolk
    Posted 4 years ago #

    Brilliant. As soon as i used get_the_date() instead of the_date() it worked fine.

    my bad for being too inpatient to read the text hha.

Topic Closed

This topic has been closed to new replies.

About this Topic