If you have created at least several WordPress sites or even read
this post , then you probably already know that WordPress is much more than just a blogging engine. It can also be used as a CMS, and WordPress widgets are a powerful tool in the arsenal of a WP developer.
Many people think that
WordPress widgets are just a way to reorganize the sidebar structure of your blog, without touching the code. This is a really useful ability of the widgets, but this is just the tip of the iceberg of all the features they provide.
1. Preparing multiple widget zones
')
Equipped with widgets, the theme has long become commonplace, both for users and developers. However, today
one widget-area on the site is no longer enough. The first step in using widgets on your WordPress site is to widget themes, and it’s not difficult if you use the correct code.
Register widget-zones
To create several zones prepared for installing widgets on your site, first of all you need to register them in the
functions.php file of your WP theme. Suppose your theme consists of three columns, and you want two separate sidebars to the right and left:
<? php
register_sidebar (array (
'name' => 'left-sidebar',
'id' => 'left-sidebar',
'before_widget' => '<div id = "% 1 $ s" class = "% 2 $ s widget">',
'after_widget' => '</ div>',
'before_title' => '<h3 class = "widget-title">',
'after_title' => '</ h3>'
))
register_sidebar (array (
'name' => 'right-sidebar',
'id' => 'right-sidebar',
'before_widget' => '<div id = "% 1 $ s" class = "% 2 $ s widget">',
'after_widget' => '</ div>',
'before_title' => '<h3 class = "widget-title">',
'after_title' => '</ h3>'
))
?>
Activating widget zones
Next, you need to place the dynamic sidebar code, in fact, in the sidebar files. As a rule, the
sidebar data is in the
sidebar.php file, but the location depends on the chosen theme. Here is the required code:
<? php if (! dynamic_sidebar ("left-sidebar")):?>
Left sidebar: this text is displayed by default ...
<? php endif; ?>
<? php if (! dynamic_sidebar ("right-sidebar")):?>
Right sidebar: this text is displayed by default ...
<? php endif; ?>
The code between PHP tags will be displayed if there are no active widgets in the corresponding widget area. For example, if widgets are not used in the left sidebar, you will see the text “Left sidebar: this text is displayed by default ...”.
Sources:
2. Widget Logic Plugin
If you do not want the same widgets to display the same on every single page of your blog, you will definitely need the
Widget Logic plugin.
After installing the plugin, a new input window will appear in the settings of each used widget - “Widget Logic”. In this field, you can enter WordPress status tags to control the display options for the widget.
In the image above, the Calendar widget is configured to appear only on the “Evil” page. But you can use many more different status tags:
- Display only on the main page: is_home ()
- Display only on post pages: is_single ()
- Display only on pages: is_page ()
- Display only on archive pages (categories, tags, etc.): is_archive ()
- Display on search result pages: is_search ()
- Display on all pages except the main one:! Is_home ()
- Display on page “Advertise” or “Contact”: is_page ('advertise') || is_page ('contact')
Just enter the necessary tags, depending on where the widgets should be displayed.
Sources
:
3. Query Posts
For those who do not know, the
query_posts template
tag is a very powerful WordPress function, with which you can control the appearance of various posts and pages in the loop. But if you would prefer not to mess around with extra PHP code, but still want to take advantage of the
query_posts tag, you can use the
Query Posts widget to display WordPress content in any way possible. After installing and activating the plugin, the new
“ Query Posts ” widget will be available in the widget menu of your WordPress site.
What can he do
- display posts with a given tag, category, author, date or any other parameter at your discretion;
- display any number of posts;
- display posts by publication date, title, ID; in ascending or descending order;
- display posts in full, excerpts or a list;
- show pages.
A source:
4. Error 404 templates
Many WordPress themes, including the default theme, cannot boast a choice of interesting and useful templates for error 404. For example, if you get to the error page on the site with the WordPress default theme, you will be greeted with the message
“ Error 404 - Not Found ” , that's all. There are a large number of widgets that will help give the page a 404th error a highlight, and make it more useful for visitors who are looking for content. Among them are the
“Recent Posts,” “Categories” and
“Archives” widgets.
Code
The first step: register the widget zone in WordPress. To do this, open your theme's
functions.php file, and put the following code there:
<? php
register_sidebar (array (
'name' => '404',
'id' => '404',
'before_widget' => '<div id = "% 1 $ s" class = "% 2 $ s widget">',
'after_widget' => '</ div>',
'before_title' => '<h3 class = "widget-title">',
'after_title' => '</ h3>'
))
?>
Now that the widget is registered, you will need to edit the
404.php file, adding this code there:
<? php dynamic_sidebar ('404'); ?>
It's all. Now you can insert any widgets into the “404” widget zone, and they will be displayed every time a visitor enters the 404th error page. Equip it with such useful things as a search field, a list of recent posts or categories, or maybe several
Query Post lists.
A source:
5. We place reclining blocks between posts.
You can add code to your theme that allows you to insert a widget through a certain number of posts. Many people use it to insert contextual advertising or banners, but in fact, the number of options for using this widget is limitless.
Code
As in the previous paragraph, the first step in setting up
the Index Insert widget will be to register the widget area for it. Open the
functions.php file and paste the following code:
<? php
register_sidebar (array (
'name' => 'index-insert',
'id' => 'index-insert',
'before_widget' => '<div id = "% 1 $ s" class = "% 2 $ s widget">',
'after_widget' => '</ div>',
'before_title' => '<h3 class = "widget-title">',
'after_title' => '</ h3>'
))
?>
To place it on the main page, open your theme's
index.php file, and enter “
endwhile ” at the end, and then paste the following code directly above it to make it look like this:
<? php if ($ count == 2) {?>
<? php dynamic_sidebar ('index-insert')?>
<? php}?>
<? php $ count = $ count + 1; ?>
<? php endwhile; ?>
The above code will insert the “
index-insert ” widget zone immediately after the second post. You can change
$ count == 2 by specifying the number of the post, after which you want to display the widget area. If you want your banners to be displayed between archive lists, for example, on category or tag pages, you can paste the above code into other files, including
archive.php ,
category.php and
tag.php . You can even control which pages will display particular ad units using status tags, such as
is_archive (), is_category () and
is_tag () , in the
Widget Logic plugin.
A source:
- Thematic wordpress theme
Original article: 5 Useful And Creative Ways To Use WordPress Widgets
Translation from the site: WordPress for everyone!