Tuts Tuesday: A Different Way to Set Default Sidebar Widgets

Tuts Tuesday is back! On this article, I will show you how to set the defaut sidebar widgets using the_widget function.

Target Audience

This article is intended to the people who have knowledge in WordPress theme development.

Code Changes

In order to have a list of pages widget on the sidebar, you have to code like this

<div class="widget widget_pages">
	<h2>List of Pages</h2>
	<ul>
		<?php wp_list_pages('title_li='); ?>
	</ul>
</div>

But, you can achieve this with the same result.

<?php the_widget('WP_Widget_Pages', array('title' => 'List of Pages')); ?>

The function the_widget saves lot of time and lines. It displays the arbitrary widget as a template tag.

Placeholder Widget

If you want to put a placeholder widget, you can do like this.

<?php the_widget('WP_Widget_Text', array('title' => 'Widget Here', 'text' => 'This is the placeholder widget.')); ?>

You can see the list of default  widget classes that can be used for the_widget function. If you have plugins that support sidebar widgets, you can make use of it too. Refer to the plugin manual on the parameters (in this case, they call it instance).

 

 

Categories: Tutorials, Tuts Tuesday

Tags: , ,

Comments

There are no comments on this entry.

Add a Comment