On wordpess how can i show recent posts on sidebar

3 replies
I have sticky posts on my site and this code doesn't work properly because of them. How can i show only recent posts on sidebar with codes.

PHP Code:
<ul><?php $posts=get_posts("numberposts=10&orderby=post_date&order=DESC"); foreach($posts as $post) : ?><li><a href=""><?php the_title(); ?></a></li><?php endforeach; ?></ul>
#posts #recent #show #sidebar #wordpess
  • Profile picture of the author rmmfree
    The code seems to be fine. Be sure to run it after the post content is displayed, not before.
    {{ DiscussionBoard.errors[10514259].message }}
  • Profile picture of the author Nathan K
    Try this code, I have not tested it.

    <?php
    $args = array(
    'post__not_in' => get_option( 'sticky_posts' ),
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'DESC'
    );
    $posts = get_posts($args);
    ?>
    <ul>
    <?php foreach($posts as $post):?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; wp_reset_postdata();?>
    </ul>
    {{ DiscussionBoard.errors[10514393].message }}
    • Profile picture of the author Hasan Fardous
      You can install a plugin 'Recent post plugin' which will show your recent posts to your sidebar.
      {{ DiscussionBoard.errors[10525338].message }}

Trending Topics