Conditional WordPress widgets with rich editable text

For the "About Joost" widget shown on the homepage of this site, I created a new widget. It has two great features: it shows just on the homepage, and the content and title are drawn from a page, so that I can edit it as a normal page with the richtext editor in the backend of WordPress.

The code basically looks like this:

[source language="php"]
function widget_about_me($args) {
if (is_home()) {
extract($args);
query_posts('page_id= ');
while (have_posts()) : the_post();
echo $before_widget;
echo $before_title.get_the_title().$after_title;
the_content('');
echo $after_widget;
endwhile;
query_posts('');
}
}
[/source]

The is_home() makes sure the widget only shows up on the homepage, and then you enter into a new loop with a specific page id. After having done that, I open the "normal" loop again, to make sure other widgets and stuff that relies on is_home() keeps working.

Of course you have to make sure that you 301 redirect the "page" you're using in the widget when it get's opened directly to your homepage. There's no reason for anyone to want to see just that content.

Related posts

  1. Ads Widget for WordPress
  2. My redesign: the homepage
  3. WordPress subpages going supplemental: the fix
  4. Switching from plain text to HTML emails
  5. Showing subcategories on WordPress category pages

Want more WordPress tips?

You should subscribe to my WordPress Newsletter, as you'll get a whole lot more WordPress tips and tricks there! Also, subscribe to this blog right now with RSS, or daily or weekly emails!

One Response to “Conditional WordPress widgets with rich editable text”

  • SEO Carly says:

    Thanks, very tricky i'm going to give this a shot. :)

    While we are on WP functionality, i'm just wondering if you had thought about or if there's any particular reason you don't have external links open in a new window?

    Often i click a link in your post to view what your referencing and close that tab.. and Joost is gone :-/

    You think i'd learn but hey, anythings likely to happen with me :-)

Hosting by:
Hosted by MediaTemple Grid Services