Title: Using get_posts in Sidebar
Last modified: August 19, 2016

---

# Using get_posts in Sidebar

 *  [ashtonfitzgerald](https://wordpress.org/support/users/ashtonfitzgerald/)
 * (@ashtonfitzgerald)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/using-get_posts-in-sidebar/)
 * I am building a theme that has built into the sidebar a call to display a set
   of random posts from a given category using the get_posts function. It is configured
   to display the post title, excerpt, and read more link.
 * It works perfectly on my main page (index.php). When I am viewing a post or page
   other than the index page it displays the post title and excerpt properly, but
   the permalink points to the current active page/post, rather than the post the
   title and excerpt are pulled from.
 * This is the code I am using:
 *     ```
       <?php
        $postslist = get_posts('numberposts=3&orderby=rand&category_name=dog-training-videos');
        foreach ($postslist as $post) :
           setup_postdata($post); ?>
   
        <div style="border-bottom:1px solid #f0f0f0;">
        <br />
        <strong><?php the_title(); ?></strong>
        <?php the_excerpt(); ?>
       <em><a href="<?php the_permalink(); ?>">watch the dog training video...</a></em><br /><br />
        </div>
        <?php endforeach; ?>
       ```
   
 * I have a feeling that this has something to do with being on the same page as
   the loop, but I’m relatively new to template tags still.
 * This theme is currently active at: [http://sitmeanssit.com](http://sitmeanssit.com)(
   the main page displays and functions properly). [http://sitmeanssit.com/contact/](http://sitmeanssit.com/contact/)
   is an example of how the permalinks go funny.
 * Any help or direction on how to tweak this is already appreciated.
 * Thanks,
    Ashton

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [huggie](https://wordpress.org/support/users/huggie/)
 * (@huggie)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/using-get_posts-in-sidebar/#post-1412471)
 * Yeah, you’re probably right. I’d only use get_posts() on the main loop and [wp_query()](http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_1)
   on the sidebar.
 *  Thread Starter [ashtonfitzgerald](https://wordpress.org/support/users/ashtonfitzgerald/)
 * (@ashtonfitzgerald)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/using-get_posts-in-sidebar/#post-1412662)
 * Thank you. I’m going to try using wp_query() and see how that works out.
 * -Ashton
 *  Thread Starter [ashtonfitzgerald](https://wordpress.org/support/users/ashtonfitzgerald/)
 * (@ashtonfitzgerald)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/using-get_posts-in-sidebar/#post-1412666)
 * It turns out that the order of how the php tags are placed affects the outcome
   dramatically.
 * Using my original code, if I place the_permalink before the_excerpt, the permalink
   points to the proper post. If it’s placed after the_excerpt, it displays the 
   active page permalink.
 * This occurs with wp_query as well.
 * This is the original get_posts code, but with the ‘watch the dog training video’
   link duplicated above the_excerpt. It displays properly above, but not below 
   the_excerpt.
 *     ```
       <?php
        $postslist = get_posts('numberposts=3&orderby=rand&category_name=dog-training-videos');
        foreach ($postslist as $post) :
           setup_postdata($post); ?>
   
       	<em><a href="<?php the_permalink(); ?>">watch the dog training video...</a></em><br /><br />
   
        <div style="border-bottom:1px solid #f0f0f0;">
        <br />
        <strong><?php the_title(); ?></strong>
        <?php the_excerpt(); ?>
       <em><a href="<?php the_permalink(); ?>">watch the dog training video...</a></em><br /><br />
        </div>
        <?php endforeach; ?>
       ```
   
 * Can anyone shed some light on this? I don’t mind just permalinking the post title,
   but now I’m curious how this is working.
 * -Ashton

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Using get_posts in Sidebar’ is closed to new replies.

## Tags

 * [excerpt](https://wordpress.org/support/topic-tag/excerpt/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * 3 replies
 * 2 participants
 * Last reply from: [ashtonfitzgerald](https://wordpress.org/support/users/ashtonfitzgerald/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/using-get_posts-in-sidebar/#post-1412666)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
