Home » Wordpress » How to add a widget in a custom theme in WordPress?

How to add a widget in a custom theme in WordPress?

Posted by: admin November 30, 2017 Leave a comment

Questions:

So the problem is like this. I am starting to learn WordPress. Now, I came across a very cool plugin (http://wordpress.org/extend/plugins/wp-tagcanvas/). Now when I use the built-in theme, and activate the plugin, I can see it on my sidebar. All good till here. Now, I try to have a custom theme of my own. I understand that all the widgets on the twenty-eleven sidebar come up due to some function calls in one of the many template files. So I decipher that I need to make similar call in my sidebar.php.

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div id="first" class="widget-area" role="complementary">

So now the “Search” widget comes up in my sidebar . And along with it I see a heading “Tag cloud” and then nothing. While, if I use Twenty-eleven theme, I get a fully-functional 3d cloud widget.

I have gone through the readme.txt file of the plugin. I have follwed the instructions there correctly, as I am able to see the cloud widget in the twenty-eleven theme.

Can anyone help me on what I am doing incorrect here or if I am missing something.

Another question that I have is how can I display a widget using its index.
I wrote these lines:

<?php $the_sidebars = wp_get_sidebars_widgets();
print_r( $the_sidebars); ?>

which give the following output:

Array ( [wp_inactive_widgets] => Array ( [0] => calendar-2 [1] => search-3 ) [sidebar-1] => Array ( [0] => search-2 [1] => tag-cloud-html5 ) [sidebar-2] => Array ( ) [sidebar-3] => Array ( ) [sidebar-4] => Array ( [0] => rss-2 ) [sidebar-5] => Array ( ) ) 

So, if I need to display, say rss widget at some place, how do I do it using it’s index?
Thanks in advance for your help.

Answers:

Apparently, I finally figured it out.

The problem was that I had missed calling wp_head action in my files. And this plugin is activated in the wp_head action. Since the action was not being called for my theme, the plugin was not getting registered and was not displaying. On the other hand, twenty-eleven theme like all other good themes, was making a call to wp_head, so the cloud was getting displayed when that theme was activated.

Regarding the call by index, I found out in other forums that the_widget function needs widget name to be passed as parameter. So this won’t work with index.
If anyone finds out otherwise, please let me know.

So, marking this question as solved.