Title: how to display previous posts
Last modified: August 20, 2016

---

# how to display previous posts

 *  Resolved [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/)
 * Hi,
 * I want to display some posts that were posted previously on the new post. So,
   every time i create a new post, the previous posts (say 5 posts) are displayed
   at the end of the new post. These 5 post are selected randomly from the previous
   posts.
 * So, is this possible?

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/how-to-display-previous-posts/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/how-to-display-previous-posts/page/2/?output_format=md)

 *  [Harry](https://wordpress.org/support/users/harmck/)
 * (@harmck)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432463)
 * It is possible, it depends on comfortable you are with PHP and wordpress.
 * You could write a simple function (it shows 5 random posts in a bullet point 
   list):
 *     ```
       <ul>
       <?php
       $postargs=array(
       'showposts' => 5,
       'orderby' => rand
       );
   
       $random_posts = new WP_Query($postargs);
       while ($random_posts->have_posts()) :
       $random_posts->the_post();
         echo '<li>'. get_the_title() .'</li>'
       endwhile;
       ?>
       </ul>
       ```
   
 * If you’re not familiar with editing themes, try searching for a related posts
   plugin.
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432483)
 * Hi Harmck
    Thank you very much for your codes. Could you please let me know where
   should i insert the code. I cannot use a related post plugin because these posts
   are not related to each other, they are just like a dictionary website. The purpose
   of my question is to create a post and try to related to ITS previous post so
   that google can understand the keywords for the previous posts. Thank you very
   much.
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432491)
 * I guess i have to post the above codes into my single.php right?
 * I have one more question i need your help:
    In the above code, the 5 random posts
   have the anchor text exactly the same with the posts’ titles (From my understanding,
   i have very limited knowledge in php)
 * My URL structure is like this:
 * [http://mydomain.com/keyword1-this-is-information](http://mydomain.com/keyword1-this-is-information)
   
   [http://mydomain.com/keyword2-this-is-information](http://mydomain.com/keyword2-this-is-information)
   [http://mydomain.com/keyword3-this-is-information](http://mydomain.com/keyword3-this-is-information)
   [http://mydomain.com/keyword4-this-is-information](http://mydomain.com/keyword4-this-is-information)
   [http://mydomain.com/keyword5-this-is-information](http://mydomain.com/keyword5-this-is-information)
 * The post titles are correspondingly the same (e.g Keyword1 This Is Information,
   Keyword2 This Is Information, Keyword3 This Is Information, Keyword4 This Is 
   Information, Keyword5 This Is Information)
 * Now i want the anchor text is only Keyword1, Keyword2,… but ignore the “this 
   is information”
 * Is it possible to do this?
 * Thanks!
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432499)
 * I have just tried adding it in my single.php in one of my WP but it does not 
   work, it shows the error
 * Parse error: syntax error, unexpected T_ENDWHILE, expecting ‘,’ or ‘;’ in /home/
   bestco13/public_html/healthyhow.net/wp-content/themes/swift/single.php on line
   52
 * In your code is there any spelling mistake? I notice there is something like “
   new WP_Query”. Should it be “new_WP_Query”?
 * Can you let me know where should i put the code? I want to put it at the end 
   of the post. My whole single.php is:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 *  [Harry](https://wordpress.org/support/users/harmck/)
 * (@harmck)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432562)
 * `echo '<li>'. get_the_title() .'</li>'`
 * Should be:
 * `echo '<li>'. get_the_title() .'</li>';`
 * It’s missing the semi colon, I modified it from existing code removing stuff 
   you don’t need. There should be no other problems. You’ll have to add the link
   using the permalink function. But that should be straightforward.
 *  [Harry](https://wordpress.org/support/users/harmck/)
 * (@harmck)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432563)
 * Correct, It should go somewhere in your single php. Where exactly though depends
   entirely on the theme you use. Try finding where the end of your post is output
   and put it after that. Your code was removed as it was too long. Do not paste
   full file code into here, use Pastebin as suggested by the mods.
 *  [Harry](https://wordpress.org/support/users/harmck/)
 * (@harmck)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432564)
 * With regards to your URL structure. Please clarify, the “this is information”
   changes per keyword, yes?
 * You can split the text by say using a specific character such as a space and 
   always display the first word which is the keyword. As long as the keyword is
   never two words this would be a relatively easy thing to do. Let me know if this
   is what you need.
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432585)
 * Hi Harmck
 * Now the code does not give error, but it only loads the title of the 5 random
   posts, not the clickable link.
 * With regard to the keyword, keyword1, keyword 2… may comprise 1 or several words,
   but the “this is information” are the same for all posts.
 * Yesterday my home internet was down, so only now i can go to work to check your
   messages. After inserting the code, my single.php looks like this
 * [http://pastebin.com/kQA0rg9Q](http://pastebin.com/kQA0rg9Q)
 * Could you help me with the keywords problem and the clinkable link problem?
 * Thank you so much
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432586)
 * Hi, i use pastebin and follow the instruction in the forum rule, but the codes
   do not appear in my post.
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432587)
 * “You’ll have to add the link using the permalink function. But that should be
   straightforward”
 * What do you mean by this? My permanent link structure is /%postname%/
 * By the way, the website i install to test the code is [http://healthyhow.net](http://healthyhow.net),
   the 5 random posts are at the end of the post, but they are not clickable.
 * My actual website that i want to add your code after i know that it works perfectly
   is [http://syndromes.howhubs.com](http://syndromes.howhubs.com)
 * Thanks!
 *  [Harry](https://wordpress.org/support/users/harmck/)
 * (@harmck)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432589)
 * To add the links using the permalink function, I meant change:
 * `echo '<li>'. get_the_title() .'</li>';`
 * To this:
 * `echo '<li><a href="'.get_permalink().'">'. get_the_title() .'</a></li>';`
 * So I described adding the simple a HTML tag around the text that is displaying.
 * As for your ‘This is Information’ problem, if you want to hide it, I would say
   delete it. Unfortunately I don’t have the time to code this for you.
 * Good luck with your website
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432590)
 * Hi Harmck
 * Thank you so much for your help. By the way, is there any thanks button that 
   i can press to thank you?
 * Thanks!
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432591)
 * It works perfectly. Now i have clikable links!
 *  [Harry](https://wordpress.org/support/users/harmck/)
 * (@harmck)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432592)
 * No problem, marking it as resolved and you just saying it was thanks enough.
 *  Thread Starter [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * (@hinhthoi)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/#post-2432593)
 * Hi Harmck
 * Sorry to trouble you again. After searching a lot, i cannot find anything related
   to the keywords problem.
 * As you mentioned above, getting the first word of the title is doable, could 
   you give me the code to get the first word? Since quite a lot of keywords are
   only 1 word. And this may be also a clue for me to search more, since i’m not
   sure how to search about this topic in google.
 * Thanks for your kind help!

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/how-to-display-previous-posts/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/how-to-display-previous-posts/page/2/?output_format=md)

The topic ‘how to display previous posts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 17 replies
 * 2 participants
 * Last reply from: [hinhthoi](https://wordpress.org/support/users/hinhthoi/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/how-to-display-previous-posts/page/2/#post-2432601)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
