Home > database >  Swap Wordpress "Widget Area" based on Page Template
Swap Wordpress "Widget Area" based on Page Template

Time:01-30

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)

CodePudding user response:

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" >',
        'after_widget'  => '<span ></span></section>',
        'before_title'  => '<h3 >',
        '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.

CodePudding user response:

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 >',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 >',
        '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.

  • Related