If you set a static page as your front page, it can be a little tricky figuring out how to link to a page that shows your latest posts. So far I have come across two solutions you can use with your WordPress blog when linking to such a page.
Solution One
Create a link in your sidebar or somewhere on your site that looks like this: http://www.yourdomain.com/?p=all You should replace the http://www.yourdomain.com/ with the location of your blog.
Solution Two
You need to create a new page template by copying an existing one. In your new page template, find this line:
<?php while ( have_posts() ) : the_post(); ?>
Then replace that line with this code:
<?php $my_query = new WP_Query( 'showposts=10' ); while ( $my_query->have_posts() ) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
The code you just added creates a loop and retrieves the latest 10 posts and will display them on a page you create with your new template.
If you know of another solution to accomplish this task, please share it with my readers and me by commenting below.
Hi,
Question: If I want to put a or whatever to a page other than wordpress, eg. another website or another directory of the same domain, to show the latest several posts, what should I do with the html/php code? I definitely cannot use the wordpress functions…
Thanks for this. Exactly what I wanted to read.
This was also exactly what I was looking for, but I had to add a bit to the script. Might be obvious to some, but this is how I set it up:
have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
<a href=””>
Aww lifesaver! Thanks a lot.
Hi Josh!
Thank you so much for the instructions. I’ve been looking for a solution so long… I’m really grateful for this!
But I’m still not 100% satisfied, I wish I could have a “Previous Entries” link or any other kind of pagination. Any idea to accomplish that?
Thanks,
Stefan
Stefan,
If you are wanting pagination, try this code (do note the use of query_posts instead of WP_Query):
<?php
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
query_posts(‘showposts=10&paged=’.$paged);
while (have_posts()) : the_post();
?>
<h2><?php the_title(); ?></h2>
<?php
endwhile;
posts_nav_link();
?>
Thanks for the instant answer, Josh! I’ve tried it bot got the following error:
Parse error: syntax error, unexpected ‘=’ in /home/.sites/84/site989/web/blog/wp-content/themes/clasictone/pageofposts.php on line 28
at this line
query_posts(โshowposts=10&paged=โ.$paged);
Any idea?
Stefan, I think the problem has to do with the single quotes. If you copy and pasted the code I provided, then delete the single quotes and retype them. I think that should fix the parse error.
Ah yes, stupid me…
Good, that works now. Now, the page looks a little different – post titles are no links and there are no post dates. Guess that’s because of the different query, right?
Stefan, the reason that only the title is displayed with no links is because that was all I included in my last example. If you would like to display the permalink, date, post content, etc. you should take a look at the WordPress Codex [ http://codex.wordpress.org/The_Loop_in_Action#Generating_the_Post ]
Just want to say a little thank you for taking the time to post this solution, its brilliant! Even though i have used it with your nav code, it works like a charm! Honestly like many others, i have been looking for this solution for months!
@Stefan If you have copied your index.php file in order to make a new template from it, the easiest way to include everything is to copy everything below the line:
<?php while (have_posts()) : the_post(); ?> (NOTE: U MUST DELETE THIS LINE OF CODE AFTER YOUR DONE COPYING)
keep copying everything and STOP just above this line:
<?php endwhile; ?> (NOTE: U MUST DELETE THIS LINE OF CODE AFTER YOUR DONE COPYING)
Now refering back to Joshs code he kindly made for you in the comments section (By the way his code needs to go below this line): <?php if (have_posts()) : ?>
and from his code just replace this line: <h2><?php the_title(); ?></h2>
with everything you just copied and thats it.
p.s I dont know what order you going to do this in but hopefully it helps somewhat and good luck LOL.
Thanks Jay! I am glad I could help.
I think the code you provided got eaten in the submit process. ๐
I am glad you could help me too! Yeah about my code being eaten up, ha-ha do your comments take any PHP code at all? or does it need any special opening and closing PHP code tags. I was hoping to re-write my comment.
I think WordPress removes any PHP code from comments but I am not 100% on that. If you want to rewrite your comment you can try typing the code without the opening and closing PHP tags and use http://www.elliotswan.com/postable/ to help make your code more friendly. You could also email me I can see about adding it for you.
@Stefan and @Josh,
Thanks and you’re right, if your adding php code to comments it won’t work unless you have created that feature.
So what I have done is just pasted that chunk of code on my blog temporarily. If you find it useful just copy it.
My blog: http://ijayen.wordpress.com/2009/09/08/response-to-josh-stauffers-lastest-posts-article
Thanks again for this great little article!
Cool… I added the code to your original comment.
Thanks for the post and all the comments. This did solve a question which just popped up 30 minutes ago. If all problems / challenges were solved this quick…
Thank you for sharing your knowledge
I thinks this is helpful but I get a problem with lastest posts in my website when I try set sticky posts. All sticky posts show in it. Pls help.
Chung, you will to set a value for
ignore_sticky_posts
in your query.