Title: Recent Posts Code
Last modified: August 18, 2016

---

# Recent Posts Code

 *  [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/)
 * I want to get some code going so I can have recent posts show up in the sidebar.
   I’d use the sidebar widget, but when I go to use it, it messes the formatting
   of everything up. So, I just wanted to add the coding directly to the main index
   page, but I don’t know what the code is.

Viewing 15 replies - 16 through 30 (of 33 total)

[←](https://wordpress.org/support/topic/recent-posts-code/?output_format=md) [1](https://wordpress.org/support/topic/recent-posts-code/?output_format=md)
2 [3](https://wordpress.org/support/topic/recent-posts-code/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/recent-posts-code/page/3/?output_format=md)

 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583322)
 * So close! It looks great now, but there is one more problem. I have three posts,
   all of which are in the same caetegory, which is the category I have set up to
   show under recent posts.
 * However, it only shows the two in the sidebar and on the main page. When I enter
   the URL for the third post, it still doesn’t show up. But when I take the recent
   posts code out, it shows up fine.
 * What could be causing this?
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583323)
 * Actually, I did have it in the wrong category, so I switched it and it shows 
   up. However, when I made a test post in a different category, it doesn’t show
   up at all.
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583325)
 * Great, so far :).
 * Well, look at the line with `query_posts`… see the `"cat=5"` in there as an option?
   That means, that only posts from category 5 (= reviews?) are being displayed.
   If you want to include other categories to the query (let’s say, category 1),
   you would modify your query_posts like this:
 * `query_posts('cat=1,5&showposts=5');`
 * Now it says: “Ask for the 5 latest posts from categories 1 and 5.”
    You can add
   as many categories as you like. Example:
 * `query_posts('cat=1,4,5,10,12&showposts=5');`
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583326)
 * I understand that, but the problem is that it doesn’t display a post on the main
   page if it isn’t included in the query_posts.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583327)
 * And it also won’t display pages. I’ll click on a page link, and it says it goes
   there, but it just shows the home page.
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583328)
 * That’s odd… and shouldn’t be. Are you sure, you didn’t forget this line **before**
   our `"while..."`…
    `<?php $myquery = $wp_query; ?>` …and this one **below** our`"
   endwhile"`…? `<?php $wp_query = $myquery; ?>`
 * If they are both there, I don’t think this loop has something to do with the 
   not appearing post on your main page.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583331)
 * This is what I have:
 * <h2>Recent Reviews</h2>
    -  <?php $myquery = $wp_query; ?>
    -  <?php
       query_posts(‘cat=5&showposts=5’); while (have_posts()) : the_post()?
      >
    - 
    -  <?php endwhile; ?>
    -  <?php $wp_query = $myquery; ?>
 * <br/>
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583332)
 * Seems fine to me. If you delete the whole thing (the code you just posted), does
   the rest of your page work as it should?
 * I’m using something very similar on my page, and it doesn’t give me any problems.
   Maybe someone else has an idea?
 * Oh, and please use the backticks (“code”-button) for code you post (explanation
   below the text area) :).
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583333)
 * Yeah, everything works perfectly normal when the new code is gone.
 * I don’t understand.
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583334)
 * In the end, immediately after ´<?php $wp_query = $myquery; ?>` try inserting 
   the following line:
 * `<?php rewind_posts() ?>`
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583335)
 * Nope, that didn’t work.
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583336)
 * Ok, delete the `rewind_posts()` line. Then modify:
 * `<?php $myquery = $wp_query; ?>`
    to -> `<?php $myquery = clone $wp_query; ?>`
 * > and
 * `<?php $wp_query = $myquery; ?>`
    to -> `<?php $wp_query = clone $myquery; ?>`
 * Now pray, that it works with wordpress 2.2! 😉
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583338)
 * No, it gives me this error:
 * arse error: syntax error, unexpected T_VARIABLE in /home/thinkth1/public_html/
   savingprogress/launchprep/wp-content/themes/blueblog-10/sidebar.php on line 50
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583339)
 * Ok, one last thing before I give up…
 * Delete:
    `<?php $myquery = $wp_query; ?>` (or the modified one with clone) and…`
   <?php $wp_query = $myquery; ?>` (or the modified one)
 * Replace:
    `<?php query_posts('cat=5&showposts=5'); ?>` with… `<?php $myquery 
   = new WP_Query('cat=5&showposts=5'); ?>` and… `<?php while (have_posts()) : the_post()?
   >` with… `<?php while($myquery->have_posts()) : $myquery->the_post(); ?>`
 * But I have no idea, if the loop shows the correct titles/links, regardless of
   what else is currently displayed on your page.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/2/#post-583342)
 * You are probably the smartest person alive. It works perfectly!
 * THANK YOU SO MUCH!

Viewing 15 replies - 16 through 30 (of 33 total)

[←](https://wordpress.org/support/topic/recent-posts-code/?output_format=md) [1](https://wordpress.org/support/topic/recent-posts-code/?output_format=md)
2 [3](https://wordpress.org/support/topic/recent-posts-code/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/recent-posts-code/page/3/?output_format=md)

The topic ‘Recent Posts Code’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 33 replies
 * 4 participants
 * Last reply from: [Sivar](https://wordpress.org/support/users/sivar/)
 * Last activity: [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/page/3/#post-583346)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
