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 - 1 through 15 (of 33 total)

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

 *  [drmike](https://wordpress.org/support/users/drmike/)
 * (@drmike)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583051)
 * need a link please….
 * The one in your profile isn’t a wp blog it appears.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583052)
 * The [template tag](http://codex.wordpress.org/Template_Tags), [wp_get_archives()](http://codex.wordpress.org/Template_Tags/wp_get_archives),
   and the postbypost parameter can get the job done.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583053)
 * [http://www.savingprogress.com/launchprep](http://www.savingprogress.com/launchprep)
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583056)
 * I forgot to mention that I only wanted to have only one category of posts show
   up in the recent posts section. Is there a way to add code to the wp_get_archives()
   template tag?
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583060)
 * I got the recent post code up and working for the most part. The one thing is
   that it isn’t formatted like the rest of the stuff in the sidebar. Did I miss
   something?
 * And, is it possible to not only have it show one category, but show the most 
   popular posts in that category? Like, say I want to show no more than 5 posts,
   but their not actually the most recent posts in that category, but the ones with
   the most comments. Possible? Not possible?
 *  [drmike](https://wordpress.org/support/users/drmike/)
 * (@drmike)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583062)
 * check out the link that MichaelH gave you as it talks about listing only certain
   categories and modify the outputted code.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583064)
 * I checked it, but I didn’t find anything that would show me how to get only one
   category of posts to show.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583082)
 * I’ve tried changing stuff around, but it hasn’t really helped. The formatting
   is still messed up. I changed my mind on the one category idea, though. Now I’d
   like to do it so it’s from a few categories, but not all categories.
 * Any ideas?
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583086)
 * You might be interested in [this](http://wordpress.org/support/topic/121231?replies=8#post-577841).
 * You should modify that code with `"<?php $myquery = $wp_query; ?>"` before the
   query_posts and `"<?php $wp_query = $myquery; ?>`” after the end of the loop 
   to preserve the original query.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583092)
 * OK, so I had this before:
 * <h2>Recent Reviews</h2>
    - <?php wp_get_archives(‘type=postbypost&limit=5&format=custom’); ?>
 * <br/>
 * Then I changed it to this:
 * <h2>Recent Reviews</h2>
    - <?php $myquery = $wp_query; ?>
    - <?php
       query_posts(‘cat=5&showposts=5’); while (have_posts()) : the_post()?
      >
    - <?php $wp_query = $myquery; ?>
    - <?php endwhile; ?>
 * <br/>
 * But it doesn’t show anything now, just the header.
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583095)
 * First of all… the line `"<?php $wp_query = $myquery; ?>"` needs to be BELOW the
   endwhile. Otherwise you get a real mess.
 * And second… of course it doesn’t show anything, because there is nothing inside
   your loop. You need to put template tags inside it like `the_title()` and `the_content()`.
 * I’ll give you an example of a very simple loop:
 *     ```
       <?php while (have_posts()) : the_post(); // watch the semicolon at the end of the line, I forgot that before! ?>
       <h2><?php the_title() ?></h2>
       by <?php the_author() ?> · <?php the_time('F js, Y') ?>
       <?php endwhile; ?>
       ```
   
 * I hope, it got it all right and could help (I’m pretty new to wordpress, too)…
   and maybe [this](http://codex.wordpress.org/The_Loop) gives you a better understanding
   of The Loop itself.
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583310)
 * All right, I put this in, doing what I think you meant:
 * <h2>Recent Reviews</h2>
    -  <?php $myquery = $wp_query; ?>
    -  <?php
       query_posts(‘cat=5&showposts=5’); while (have_posts()) : the_post()?
      >
    - <?php while (have_posts()) : the_post(); // watch the semicolon at the end
      of the line, I forgot that before! ?>
       <h2><?php the_title() ?></h2> by <?
      php the_author() ?> · <?php the_time(‘F js, Y’) ?>
    -  <?php endwhile; ?>
    -  <?php $wp_query = $myquery; ?>
 * <br/>
 * But I get this error when I load the page:
 * Parse error: syntax error, unexpected T_ENDIF in /home/thinkth1/public_html/savingprogress/
   launchprep/wp-content/themes/blueblog-10/sidebar.php on line 78
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583316)
 * Delete the following line:
    `<?php while (have_posts()) : the_post(); // watch
   the semicolon at the end of the line, I forgot that before! ?>` You got that `"
   while..."` twice, when you copy-pasted my loop example.
 * After that, see if the error message still shows up. If it does, there must be
   some `"endif;"` where it does not belong (I would guess, below the code you posted).
   Try commenting that out, unless it belongs to another loop, which starts with`"
   if (have_posts())"`.
 * Good luck! 😉
 *  Thread Starter [mithrustt](https://wordpress.org/support/users/mithrustt/)
 * (@mithrustt)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583317)
 * It kinda works now. The only problem is I just want it to be a link, like the
   other links in the sidebar, and not a header, like the headers in the sidebar.
 * Also, the text doesn’t space out properly. Like, it overlaps.
 *  [Sivar](https://wordpress.org/support/users/sivar/)
 * (@sivar)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-code/#post-583320)
 * Ok, try something else as content of your loop. Content means, between:
    `<?php
   while (have_posts()) : the_post() ?>`
 * > and
 * `<?php endwhile; ?>`
 * Here’s the content:
    `<li><a href="<?php the_permalink() ?>" title="<?php _e('
   Permanent link to'); ?> <?php the_title(); ?>"><?php the_title() ?></a></li>`

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

1 [2](https://wordpress.org/support/topic/recent-posts-code/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/recent-posts-code/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/recent-posts-code/page/2/?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
