When Twenty Eleven theme is activated, it inserts several default widgets into its sidebars. How can I code my theme to do the same?
Leave a Reply
You must be logged in to post a comment.
When Twenty Eleven theme is activated, it inserts several default widgets into its sidebars. How can I code my theme to do the same?
You must be logged in to post a comment.
Johannes Pille is right TwentyEleven’s widgets such as Archives and Meta are hardcoded into their sidebar.php
Here is what I would do:
First define your sidebars in your functions.php like so:
As you can see in the above code you are naming the sidebar “Left Sidebar”. You can change this to whatever you want but I generally choose to use placement terms so that way I know what part of the page the widget will show on.
Next you will want to put this in your theme where you want the sidebar to show:
Now you will want to put this next:
The
closes the “if” function at the beginning of this string. What this will do is if there are no widegts place in the sidebar these default ones will appear until one is added.
If you want them there all the time without people changing it but still want them to be able to add content of their own you can just place the hardcoded widgets outside the “if” statements like this:
I will suggest in order to be adding these to your theme (in the case you want something else) that you look at the WordPress Codex Function reference here: http://codex.wordpress.org/Function_Reference#Theme-Related_Functions
…………………………………………………………………………………………………………………………………………………
Here is how to have a set of default widgets for in the admin area.
First you must know that WordPress comes with 12 standard widgets. You can view them here: http://codex.wordpress.org/Function_Reference/the_widget
If you want to remove some of the standard widgets here is how you go about doing so:
You would add the above code into your functions.php file of your theme. This will hide them. (not necessarily remove them)
If you want to add a custom widget to the admin area i would suggest that you read this article on how to register a new widget.
http://dev7studios.com/resources/wordpress-development-for-designers-part-1/
In your themes sidebar pages you can can check to see if a widget is active in your sidebar, and then do an if, else.
The (pseudo-)widgets titled “Archives” and “Meta” are hardcoded into TwentyEleven’s sidebar.php.