I had tried out the code Recent WordPress Post in Sidebar suggested by EngLee. Although the code did shows the recent posts correctly but some functions (eg: is_page()
and is_home()
are not working properly when it is after the portion of the code. My guess is the wordpress loop in the code modified value of some global variables.
I have a workaround for it. Before using the is_home()
or is_page()
function, I save the returned in a temporay variable, call the recent posts loop, and use the temporary variable later when neccessary.
Today when I search for wp_get_archives()
in WordPress WIki, I found that I can generate recent wordpress posts in the more easier way. Here is the code:
<li>
<h2>Recent Posts</h2>
<ul>
<?php wp_get_archives('type=postbypost&limit=10'); ?>
</ul>
</li>
Simple and tidy :)
EngLee says
LOL. That function is so much easier!
As for the is_home() or is_page() functions, I also faced the same problem. But I fixed it another way.
if (is_home()) {
} else if (is_page()) {
}
That would do the job. This means that if you want something to appear in home and page, you have to write it in both cases.
LcF says
wordpess wiki is OLD. you should visit codex.wordpress.org for documentation.
Here is the updated documentation for wp_get_archives:
http://codex.wordpress.org/Template_Tags/wp_get_archives
Dawn Isaac says
Awesome Thanks.
uri says
Hi,
could you explain your workaround for the is_page() disfunctioning after using EngLee’s method? I’m only a beginner with PHP so I couldn’t understand how to solve it from the explanations you both gave here.
My problem is like that: I have a footer which can be wide (if it is a wide page) or narrow (for narrow page). I used EngLee’s method in this page:
http://uriashi.com/?page_id=38
(in the left sidebar, you can see I even used the_excerpt_rss() instead of the_title() in order to display the images that I put for each post)
in the footer there’s an conditional if that says something like “if it is page 38 – display the wide footer”, but as you can see, after implementing the query_posts it messed it as you said and the footer displayed is the narrow one.
thanks in advance,
uri
szehau says
Hi uri,
Let say you have a code does something like this:
// Call query_posts
query_posts ("whatever");
if (is_page()) {
// dispaly A
} else {
// display B
}
Instead of doing that, change to this:
// Save whatever is_xxx() function
$is_page = is_page();
$is_post = is_post();
// Call query_posts
query_posts ("whatever");
if ($is_page) { // the different is here
// dispaly A
} else {
// display B
}
if ($is_post) { // the different is here
// dispaly C
} else {
// display D
}
Hope this will help :)
uri says
It does! thanks a lot!
PeterVk says
I know this post is a bit old – but thanks! Worked perfectly. Wish I’d found this earlier in the morning…