I'm working on creating a WordPress blog using my own custom theme and am having difficulty displaying widgets in the sidebar. I have registered my sidebar in functions.php, and am using get_sidebar(); in index.php. If I put content in sidebar.php it will correctly display in the sidebar, but I want to be able to modify the sidebar dynamically. However, when I go to Appearance > Widgets, it shows my sidebar name, but no widgets are listed in the "Available Widgets" section.
This is where I register my sidebar in functions.php:
function blog_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'blog' ),
'id' => 'sidebar-1',
'description' => '',
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
) );
}
add_action( 'widgets_init', 'blog_widgets_init' );
This is where I call the sidebar in index.php:
<?php get_sidebar(); ?>
And this is sidebar.php:
<?php
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div>
Any ideas why there no widgets available in Appearance > Widgets?
Edit: Solved it! There was actually a place in my code where the default widgets were being disabled. I didn't realise that was there, as it must've been included in the theme that I was basing mine off of. Removed those functions and the widgets show up again.
I think part of the issue is the call to is_active_sidebar()
From the documentation of that function:
Any sidebar that contains widgets will return TRUE, whereas any
sidebar that does not contain any widgets will return FALSE.
So your first line, since that sidebar contains no widgets (yet), will always just "return" and end any further processing in sidebar.php. I'd probably just remove that whole block all together.
I don't exactly know why you're not seeing widgets show up in the admin area, but I'd be curious what happens if you retry after the above change.
Related
i'm using 3 widget areas in my child theme's footer.
i created a secondary custom theme successfully, and am hoping i can simply change those 3 widget area's out in the second theme.
"Footer - Column 1" would change to "Alt Footer - Column 1"
"Footer - Column 2" would change to "Alt Footer - Column 2"
"Footer - Column 3" would change to "Alt Footer - Column 3"
(and yes, i've already created in WP admin > Appearnace > Widgets, those 3 new custom widget areas, & put temp text widgets in them for now).
i'm using this in "functions.php" to change the "Menu"...
add_filter( 'wp_nav_menu_args', 'respect_menu_swap' );
function respect_menu_swap( $args = '' )
{
if(is_page_template('template-respect.php') && $args['menu_id'] == 'avia-menu')
{
$args['menu'] = '16';
}
return $args;
}
...am hoping for something similar to change each of those "widget areas".
i've searched, read, searched, & read a LOT trying to figure this out & just can't seem to grasp what should go in place of the menu terms in that code bit. i really struggle with PHP, so would greatly appreciate specific explanation & code:-)
and, i'm saying "Widget Area" instead of "Widgets" because i realized that "Widgets" are INSIDE the "Widget Areas". i'd like to swap the whole area instead of just the widget so my people can add/remove various widgets in those 3 "areas" in the WP > Appearance > Widgets admin page as needed. it’s my understanding that if i just use the “Widget ID” then when someone changes which widget(s) are in one of those widget areas, it won’t update on the sites front-end without me changing those ID’s first. i’d like to avoid having to do that if possible.
(BTW: i'm using the Enfold WP theme, if that matters)
WOW!
well, it would seems as tho i figured it out!
after MORE searching, i came across https://learn.wordpress.org/lesson-plan/widget-areas
apparently i had to "register" a "sidebar" - which thru me, as i was wanting to modify in the footer. after looking that up, the term "sidebar" is used anywhere the theme allows the user to add widgets in. and since the Enfold theme footer allows for widgets, i had to "register_sidebar" and THEN modify a line in the template.
i also didn't understand why i had to "register" the "widget area's" at all, thinking that just by creating the custom widgets in WP admin > Appearance > Widgets, that should do it, but no. ya gotta create them there and ALSO register them in the child functions too.
so after reading that wordpress page (linked above), i started hunting around in the main Enfold theme files, trying to find something similar. since apparently, some things are named differently than in other themes, which is why my copy/pasting of examples i found elsewhere didn't work.
i found it in... themes > enfold > includes > admin > register-widget-area.php
so i combined what that wordpress link offered with the footer widget registration and came up with this code that i put in my child functions.php...
add_action( 'widgets_init', 'MYWIDGETAREA' );
function MYWIDGETAREA()
{
register_sidebar( array(
'name' => 'Respect Footer - Column ' . $i,
'id' => 'espect_foot_widget_' . $i,
'before_widget' => '<section id="%1$s" class="widget clearfix %2$s">',
'after_widget' => '<span class="seperator extralight-border"></span></section>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>'
));
}
in the array, i only changed the "name" & "id", leaving the rest so the structure matches the original theme.
then i went to my template-custom.php and found this line...
if( ! ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Footer - Column ' . $i ) ) )
...and replaced it with...
if( ! ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Respect Footer - Column ' . $i ) ) )
AMAZING!!!
well hopefully all this may benefit someone else in a similar position some day.
If you just want a different widget area for each page template, you can just register a widget area for each one, since the page template would contain it.
But, if you're using the same footer file on multiple page templates and want to load different widget areas dynamically, you would first have to register widget areas with the page template slugs included in the ID, for example:
function custom_register_widget_area() {
register_sidebar( array(
'name' => 'Template Respect Footer',
'id' => 'template-respect-footer',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'custom_register_widget_area' );
Adding another register_sidebar() for each template.
Then in the footer file (using template-respect.php from your example) you can get the basename of the template to build the widget area ID:
$template = basename( get_page_template_slug( get_the_id() ), '.php' );
if ( $template && is_active_sidebar( $template . '-footer' ) ) {
dynamic_sidebar( $template . '-footer' );
} elseif ( is_active_sidebar( 'default-footer' ) ) {
dynamic_sidebar( 'default-footer' );
}
This would load whichever widget area matches the page template slug, or the default if the default page.php is being used.
I'm a Wordpress noob and trying to figure out the best way to show different content in the side bar only on home page. I have set the front page to be static and right now it shows the usual widgets from the blog posts page. I just need to show some custom content in the sidebar only on the home page. So far I have found solutions to install plugins, multiple sidebars etc. I was thinking more along the lines of having some conditions to show different content within the existing sidebar. Can someone please advise how can I achieve this task?
Thanks!
simple way is to edit sidebar.php file and find where you want to add sidebar (for example at the top) and check if is_front_page() then use a function to check if is_active_sidebar('frontpage_sidebar') and else the rest of global widgets.
after that you should register a sidebar named frontpage_sidebar.
functions.php
$args = array(
'name' => __( 'Sidebar name', 'theme_text_domain' ),
'id' => 'frontpage_sidebar', // ID should be LOWERCASE ! ! !
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' );
register_sidebar($args);
sidebar.php
if(is_front_page()){
if(is_active_sidebar('frontpage_sidebar')){
dynamic_sidebar('frontpage_sidebar');
}else{
echo 'please set a sidebar for your frontpage';
}
}else{
if(is_active_sidebar('sidebar')){
dynamic_sidebar('sidebar');
}else{
echo 'please set default widgets for whole pages';
}
}
i didn't check this code, so please check and let me know if it works.
I'm trying to get a different sidebar to load within child pages of a particular parent without the use of a plugin or setting up another template file.
This is what I have so far:
register_sidebars(1, array(
'name' => 'Other Sidebar',
'id' => "other-sidebar",
'before_widget' => '<li class="widget">',
'after_widget' => '</li>',
'before_title' => '<span class="widgettitle">',
'after_title' => '</span>'
));
if ( $post->post_parent == '1164' ) {
dynamic_sidebar( 'other-sidebar' );
}
But I'm wondering if I need use a filter of some sort to replace the default sidebar that is being loaded instead? Not too sure if that's correct or not.
Since you can edit the theme (or child theme) you could add a Page Template that would override the current page template with the custom sidebar you want.
<?php
/*
Template Name: Custom Sidebar Page // All versions
Template Post Type: page // 4.7+ only
*/
// Page code here with the sidebar you want...
?>
<aside id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'other-sidebar' ); ?>
</aside><!-- #secondary -->
Then, in the edit for your page and it's child pages, set this template as the one you want.
I know this topic has been discussed quite a bit but I've tried everything I can find online and nothing is working. I need a single widget area in my custom template. I wrote if from scratch and didn't use a barebones type starter therefore I didn't have a functions.php to start from.
In wp-admin it shows my widget as it should but when there is a widget in that area and the page is reloaded it resets. in other words the widget doesn't persist in the area it's supposed to. am I missing something? is my wordpress install bad?
here is my functions.php in it's entirety
<?php
register_sidebar
(
array(
'name' => 'Header Widget',
'id' => 'headerBanner',
'before_widget' => '<div id="banner">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
)
)
?>
and my index.php
<?php if ( !dynamic_sidebar (1)) : ?>
<h1>it didn't work</h1>
<?php endif; ?>
try:
<?php if(!dynamic_sidebar ('headerBanner')) : ?>
<h1>it doesn't work</h1>
<?php endif; ?>
You need to pass the id of the sidebar you want to display when calling dynamic_sidebar
Also note that dynamic_sidebar will return false if there are no widgets added to the sidebar so add a widget to it and see if it works
Have you tried this other approach?
<?php dynamic_sidebar( 'headerBanner' ); ?>
i'm using the Showcase Sidebar with a Text widget. I've inserting some HTML and Text and want that Text widget to display. What PHP code do I insert into the sidebar.php template to show the Text widget and any other ones that I add? Ideal I would like to only load the first Text Widget in from the Showcase Sidebar
would the code look something like this?
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('sidebar2') ) : ?>
<?php endif; ?>
yes, it would look something like code you posted:
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('sidebar2') ) : ?>
<?php endif; ?>
, but -
The code that you posted will only work if you have defined and registered such a sidebar (named : sidebar2)
in your theme´s php you should have a register_sidebar() function call.
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name'=> 'Sidebar 2',
'id' => 'sidebar2',
));
}
you can even customize it further with other parameters like .
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="offscreen">',
'after_title' => '</h2>',
If your theme already have such a sidebar defined and registered , then the code you have posted will work and the front side will display all the widgets that you put to that sidebar (on the admin side) .
If it is not defined by your theme you will need to define it or ADD it to the definitions of other sidebars .
Read more on the codex here and here about defining sidebars.