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:

function widget_about_me($args) {
if (is_home()) {
extract($args);
query_posts('page_id=<pageid>');
while (have_posts()) : the_post();
echo $before_widget;
echo $before_title.get_the_title().$after_title;
the_content('');
echo $after_widget;
endwhile;
query_posts('');
}
}

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.

Tags:


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

1 Responses

  1. SEO CarlyBy SEO Carly on 10 October, 2007

    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 :-)