Doing more with Widgets: Home Page Layout

Posted: 23rd Oct 2007, in: Organisation, Widgets - Older Post - Newer Post

I have been several versions of Wordpress behind for a little while and, with this blog, have just caught up. My verdict? Widgets are pretty cool. Having said that it seems clear that the creators of widgets have missed a trick. Why stop with the sidebar?

Widgets give you control over the layout and contents of your sidebar, but there is no reason why you can’t use them to control other parts of your site. For example, in just ten minutes I was able to convert my home page to give me control over most of the layout. Now I am going to explain how.

Register a second sidebar

In the functions.php file, in my theme folder, I have now registered two sidebars. One is controlling my footer, and the other the content of my home page;

if ( function_exists(‘register_sidebar’) ) {
    register_sidebar(array(
	‘name’ => ‘footer’,
        ‘before_widget’ => ‘<div class="segment">’,
        ‘after_widget’ => ‘</div>’,
        ‘before_title’ => ‘<h3>’,
        ‘after_title’ => ‘</h3>’,
    ));
	register_sidebar(array(
	‘name’ => ‘home page contents’,
        ‘before_widget’ => ,
        ‘after_widget’ => ,
        ‘before_title’ => ,
        ‘after_title’ => ,
    ));
 
}

I’ve given then nice names so I recognise them in the admin console.

Create and register the home page content widget

Again in functions.php, I have produced a function to output the standard contents of the home page loop without any change. The example has a placeholder for simplicity:

function widget_home_page_content(){
 
?>
	<?php if (have_posts()) : ?>
 
		<?php while (have_posts()) : the_post(); ?>
 
		*** Loop Contents ****
 
	<?php else : ?>
 
 
 
	<?php endif; ?>
<?php
 
}

I have then registered this function as a new widget

register_sidebar_widget(‘Home Page Content’,widget_home_page_content);

Pretty simple so far right?

Convert your index to a sidebar

I have replaced the Wordpress loop in my index file, with a call to the second sidebar. My entire index file now looks like this:

<?php get_header(); ?>
 
	<div id="content">
	<?php if ( function_exists(‘dynamic_sidebar’) ) { dynamic_sidebar(2); } ?>
	</div>
 
<?php get_sidebar(); ?>
 
<?php get_footer(); ?>

I can now start to produce and use widgets to place content above and below the contents of my home page, without messing with the theme. I could even go further and break the loop into parts, giving even more control over the precise way posts are displayed, or make a single page theme, using widgets to control the layout.

I’m just wondering how far it is possible go using only widgets.

 

Comments

  1. 1

    Christoph Voigt (http://www.bastelbude.com) commented at 8:35 pm, 11th 11 2007:

    I guess you could go pretty far with creating a theme that’s completely made out of widgets. However, the gain in flexibility comes with the price of increased administration.
    If I’m not mistaken every widget has to be installed as a plugin. Really makeing use of a widget-based theme could easily create a dozen of widgets which will sooner or later clutter the plugins-page. Wouldn’t it be great if you could have a sort-of-wysiwyg editor for managing the entire theme?

     
  2. 2

    Andrew Rickmann (http://www.arickmann.co.uk) commented at 11:11 pm, 11th 11 2007:

    Christopher, it is possible to install widgets as part of the functions.php file that sites in the template itself, so other than installing the template there isn’t really too much more you need to do.

     
  3. 3

    CarpetGuy (http://opinionhead.com) commented at 11:16 pm, 20th 01 2008:

    I have one question, what happens when you upgrade? The way that I understand it, your additions, not done as a plug-in, will get over written when you upgrade. Might it be better to create a plugin instead of just changing code?

     
  4. 4

    Andrew Rickmann (http://www.arickmann.co.uk) commented at 7:51 am, 21st 01 2008:

    CarpetGuy,

    You could do it as a plugin but this technique relies on changing the theme itself so if you changed the theme you are likely to lose most of it anyway.

    Upgrading WordPress shouldn’t have an effect on this technique though as the only changes are to the theme files.

     

Other blogs writing about this

  1. wp-fun » Blog Archive » Doing more with widgets: Changing Layouts 13th 11 2007 at 9:05 am

    [...] few weeks ago I showed how the home page contents could be moved into a widget to allow additional content to be added above and below it. That’s an interesting technique [...]

     
  2. deshock.com » Blog Archive » We Need To Kill The Sidebar 05th 05 2008 at 2:48 pm

    [...] Developers: Check out Doing more with Widgets: Home Page Layout and Doing more with widgets: Changing Layouts for some fun tutorials on using WordPress [...]

     

Leave a Reply

I am currently testing a comment link policy which means commenters do not get a link. There is a poll, and open comments for feedback on the comment policy page.

Please note. I am currently using an experimental antispam technique on this blog. If you run into problems please let me know using the Get in Touch link at the top of the page. Thanks, Andy.

Subscribe without commenting

Feed Icon - Get fed with RSS