free website hit counter
RSS Feed     Twitter     Facebook
Room 09 Shop
Read Detail
Live Demo
Buy Now
Simplicity eCommerce
Read Detail
Live Demo
Buy Now
Bazar Shop
Read Detail
Live Demo
Buy Now
The Retailer
Read Detail
Live Demo
Buy Now
Phomedia E-Commerce
Read Detail
Live Demo
Buy Now

Use WordPress Widgets By 5 Useful And Creative Ways

Font size:
  • Sharebar

If you’ve been reading some of the previous WordPress-related articles here on Smashing Magazine, you’ll know that WordPress is much more than a blogging platform. It can be used as a CMS, too. And WordPress widgets are a powerful tool in your WordPress development arsenal.

When you think of WordPress widgets, you may think they’re just a way to rearrange various items in your blog’s sidebar without touching any code. Sure, that’s nice and all, but that’s really just the tip of the iceberg of all the things WordPress widgets can do.

You may be interested in the following related posts:

(Smashing’s note: Subscribe to the Smashing eBook Library and get immediate unlimited access to all Smashing eBooks, released in the past and in the future, including digital versions of our printed books. At least 24 quality eBooks a year, 60 eBooks during the first year. Subscribe today!)

1. Multiple Widget-Ready Areas

Screenshot

A widgetized theme has come to be expected by theme users and developers alike. Nowadays, however, just one widgetized area doesn’t cut it. The first step to using widgets on your WordPress website is to widgetize your theme, and that’s really not that hard if you have the right code in place.

Register the Widget Areas

To have multiple widget-ready areas, the first thing to do is register the widget areas in the functions.php file of your WordPress theme. Let’s say you have a three-column theme, and you want to have two different sidebars on the left and right side:

01 <?php
02     register_sidebar( array(
03         'name' => 'left-sidebar',
04         'id' => 'left-sidebar',
05         'before_widget' => '<div id="%1$s">',
06         'after_widget' => '</div>',
07         'before_title' => '<h3>',
08         'after_title' => '</h3>'
09     ) );
10     register_sidebar( array(
11         'name' => 'right-sidebar',
12         'id' => 'right-sidebar',
13         'before_widget' => '<div id="%1$s">',
14         'after_widget' => '</div>',
15         'before_title' => '<h3>',
16         'after_title' => '</h3>'
17     ) );
18 ?>

Activate the Widget Areas

The next step is to put the dynamic sidebar code in the actual sidebar files. Depending on your theme, this could be located in a sidebar.php file, or elsewhere. Here’s the code to use:

1 <?php if (!dynamic_sidebar("left-sidebar") ) : ?>
2 Default left sidebar stuff here…
3 <?php endif; ?>
1 <?php if (!dynamic_sidebar("right-sidebar") ) : ?>
2 Default right sidebar stuff here…
3 <?php endif; ?>

The code between the PHP tags will be displayed if no widgets are currently used in the relevant widget area. For example, if no widgets are used in the “left-sidebar” widget, then “Default left sidebar stuff here…” would be displayed instead.

Sources:

2. Widget Logic

Screenshot

Sometimes you may not want the same widgets to appear the same way on every single page of your blog. This is where the Widget Logic plug-in comes in handy.

After the plug-in is installed, a new “Widget Logic” input box is displayed in the options box of every widget you use. In this box, you can type a series of WordPress conditional tags to control where exactly the widget is displayed.

In the screenshot above, the Calendar widget is set to display only on the page called “Evil.” You can use many more conditional tags as well.

Examples

  • Display only on the home page: is_home()
  • Display only on individual posts: is_single()
  • Display only on pages: is_page()
  • Display on archive pages (category, tag, etc.): is_archive()
  • Display on search results pages: is_search()
  • Display on all pages except the home page: !is_home()
  • Display on “Advertise” or “Contact” page: is_page('advertise') || is_page('contact')

Simply type something similar in your own Widget Logic boxes, depending on where you want the widgets to display.

Sources:

3. Query Posts

Screenshot

For those who don’t know, the query_posts template tag is a powerful WordPress function that you can use to control how different posts and pages show up in the loop.

However, if you’d rather not mess around with more PHP code than is necessary but still want to take advantage of the query_posts tag, you can use the Query Posts widget to display WordPress content in almost any way you can think of.

After installing and activating the plug-in, you will have a new “Query Posts” widget available in the WordPress widgets menu.

What It Can Do

  • Display posts by tag, category, author, time/date or custom field keys/values,
  • Display however many number of posts you want,
  • Order posts by date published, title or ID in either ascending or descending order,
  • Display posts with full content, as excerpts or in a list,
  • Show WordPress pages.

Source:

4. 404 Templates

Screenshot

A lot of WordPress themes out there, including the default WordPress theme, don’t offer anything useful in their 404 templates. For example, if you land on a 404 page of a WordPress website that uses the default theme, you’ll be greeted with a message that says “Error 404 – Not Found,” and that’s it.

There are plenty of WordPress widgets you can use to spice up your 404 page and make it more useful to visitors who are trying to find content. The widgets “Recent Posts,” “Categories” and “Archives” come to mind.

The Code

The first step is to register the widget area in WordPress. To do this, open up your theme’s functions.php file, and put the following code in it:

01 <?php
02     register_sidebar( array(
03         'name' => '404',
04         'id' => '404',
05         'before_widget' => '<div id="%1$s">',
06         'after_widget' => '</div>',
07         'before_title' => '<h3>',
08         'after_title' => '</h3>'
09     ) );
10 ?>

Now that the widget is registered, you’ll have to edit your theme’s 404.php file and add the following code:

1 <?php dynamic_sidebar( '404' ); ?>

That’s it. You can now place any widgets you’d like in your “404? widget, and they will be displayed every time a visitor lands on a 404 page. Load it up with things like a search box, recent posts, category listings or maybe a few Query Post lists.

Source:

5. Insert Ads Between Posts

Screenshot

You can code your theme to insert a widget in between a certain number of posts. Some people use this to insert advertising, but the sky is the limit when it comes to widgets.

The Code

As we did when widgetizing the 404 template, the first step to setting this up is to register the widget area for the index insert. Open up your functions.php file again and insert this similar code:

01 <?php
02     register_sidebar( array(
03         'name' => 'index-insert',
04         'id' => 'index-insert',
05         'before_widget' => '<div id="%1$s">',
06         'after_widget' => '</div>',
07         'before_title' => '<h3>',
08         'after_title' => '</h3>'
09     ) );
10 ?>

To set it up on your main index page, you’ll need to open up the index.php file of your theme, locate the “endwhile” near the end and put the following code directly above it, so that it looks like this.

1 <?php if ($count==2) { ?>
2 <?php dynamic_sidebar('index-insert') ?>
3 <?php } ?>
4 <?php $count = $count + 1; ?>
5 <?php endwhile; ?>

The above code will insert the “index-insert” widget area right after the second post. You can change the $count==2 number to something else, according to the post you’d like the widget area to be displayed after.

If you want ads to show up in between your archive listings, such as on category or tag pages, you may have to insert the above code in other files, including archive.php, category.php and tag.php. You can even control which ads are displayed on which pages by using conditional tags, such as is_archive(), is_category() and is_tag(), in the Widget Logic plug-in.

Source:

More Widget Resources

Related posts

You may be interested in the following related posts:

Source: http://wp.smashingmagazine.com/2009/07/14/5-useful-and-creative-ways-to-use-wordpress-widgets/

Share with: Twitter Delicious Facebook Digg Stumbleupon Wordpress Googlebuzz Myspace Gmail Newsvine Favorites More
You can leave a response, or trackback from your own site.
Some More Popular News

Leave a Reply

 

Recent Search

TheTechjournal.com
Copyright© 2014 WordPress Planet | All Right Reserved.