Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like
Creating Named Widgets in WordPress Made Simple
Creating your own widgets in WordPress might seem like a complicated or confusing task but the truth is, it’s easier than you think!
In your the root of your theme directory (ex: wp-content/themes/default), you’ll find a file called “functions.php”, if you do not, create it.
Inside add:
if ( function_exists('register_sidebar') ) {
register_sidebar(array("id" => "facts_and_stats",
"name" =>"Facts and Stats"
));
}(all attributes can be found here: http://codex.wordpress.org/Function_Reference/register_sidebar)
In the backend, under “appearance > widgets”, you’ll find all available widgets and all available sidebars. You can drag a widget into the sidebar and customize it.
Then in a template file in your theme, say your “index.php” file, you can call the content of this widget with the following block of code:
< ?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar("facts_and_stats") ) { } ?>Widgets are wrapped in <li></li> by default, you can custom a group of them with a trick such as the following, where I remove all wrappers for multiple widgets by creating an array setting the before_ and afters_’s to nothing and merge it with the id and name info in another array I create inline.
$widget_args=array(
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
);
if ( function_exists('register_sidebar') ) {
register_sidebar(array_merge($widget_args,array("id" => "facts_and_stats", "name" =>"Facts and Stats")));
register_sidebar(array_merge($widget_args,array("id" => "report_card", "name" =>"Report Card")));
}Another trick I I like to do is add a quick link on the frontend for to the edit page if the user is logged in an has rights to edit widgets:
<?php if (current_user_can('manage_options')) { echo '<a href="'.get_bloginfo('url').'/wp-admin/widgets.php" title="edit this widget">edit this widget</a>'; } ?>| Print article | This entry was posted by Tyler Mulligan on May 16, 2010 at 6:22 pm, and is filed under Uncategorized. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 9 months ago
Oui vous le talent :)