Display 5 recent pages?
-
How can I display the 5 most recent pages (not posts) in the sidebar?
Thank you.
-
Thanks Esmi. Checked that page already, but i couldn’t figure it out how to do it with pages. Could you post the exact code?
Try something like:
<ul class="recent-pages"><?php $args = array( 'posts_per_page' => 5, 'post_type' => 'page'); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <li><a>"><?php the_title(); ?></a></li> <?php endforeach; ?>[Untested]
unfortunately it is not working 🙁
I tested and changed it a little, try it with this:
<?php $args = array( 'posts_per_page' => 5, 'post_type' => 'page'); $postslist = get_posts( $args ); if($postslist) : ?> <ul class="recent-pages"><?php foreach ($postslist as $post) : setup_postdata($post); ?> <li><a><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php endif; ?>Doesn’t work. This is what I get: http://img13.imageshack.us/img13/9085/unled23n.png
I just copy pasted what you said as a sidebar widget. Have I done something wrong?
This is in sidebar.php right?
Can you paste and submit the full code of sidebar.php of your theme into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.found out it works if I add it to the sidebar file. Thanks guys 🙂
There is another problem now.
keesiemeijer i am using your version. It shows the titles of the most recent pages but it doesn’t link to anything. How can I fix it?FOUND THE SOLUTION!
Add this to sidebar.php:
<?php $args = array( 'posts_per_page' => 5, 'post_type' => 'page'); $postslist = get_posts( $args ); if($postslist) : ?> <ul class="recent-pages"><?php foreach ($postslist as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php endif; ?>Thanks to esmi and keesiemeijer
Try changing this:
<li><a><?php the_title(); ?></a></li>to this:
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>Your code is even better, well done! Glad you got it resolved.
The topic ‘Display 5 recent pages?’ is closed to new replies.