Customizing the Recent Posts WordPress widget

So, I had a request to customize the recent posts widget on a WordPress site that I do work for. Basically, the site has two authors, each with their own section, but the recent posts widget was showing posts from both authors, instead of only from one.

I thought customizing the widget would be a simple manner of adding something in the widget logic space available in the wp admin back end, but it turned out to be a bit more involved. To start with, I had to get the PHP code widget plugin which let’s you insert php code into a widget (I could manually add the code on the sidebar.php file, but this seemed more customizable).  Then I placed the following in the widget:

<ul>
<?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5&author=4'); // add whatever parameters you want here
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

This code worked fine, but I also only wanted to display this recent posts widget on a certain page (ie the authors). I have the author page on an actual page in the site, so I thought it would be a simple matter of adding is_page() to the widget logic, but that wasn’t working. It frustrated me a bit until I found out what the problem was.

Basically, even though wordpress was on a page.php template, it wasn’t viewed that way. Instead the page was being viewed as a category, and archive, because I had inserted some category loops into the page.php template. To see how wordpress views a page, whether as is_home, is_category, is_search, is_page and so on, I was directed to a very helpful plugin called Askapache what is this.

The Askapache plugin shows how wordpress views the page and explained why the is_page() wasn’t working in the widget logic. The solution to this was to put the following:

<?php wp_reset_query(); ?>

Before the recent posts query began. I tried pasting the wp_reset_query directly above the recent posts query in the widget but that didn’t work. It did however work when I pasted the wp_reset_query at the bottom of the page.php template in question.

Anyway, it was just interesting for me to see that wordpress doesn’t always view a Page.php file as a Page, which is something I never knew before.

Posted in Wordpress | 1 Comment

Top 10 reasons why working at home sucks

slacking-off-photo

For many people, a dream job is one in which they don’t even have to leave the house to do it. It’s no surprise that all of those get rich quick scam ads on TV advertize cushy jobs where you can earn thousands without even leaving the comfort of home. Think about it, no commute, no overbearing boss, and no uptight dress code. Working from home really does sound like the ideal office job, but, wait a minute, it’s not as glamorous as it seems. As someone who’s worked from home for over three years, let me be the first to admit that doing so definitely has its benefits, but also its share of often major disadvantages.
Continue reading

Posted in Work related thoughts | Tagged , | 1 Comment