Swap Wordpress "Widget Area" based on Page Template - php

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.

Related

Add the custom widget value to the array on wordpress website

We have a home page banner in our WordPress website. Added a logic to show different images on page refresh.
The code for the images are placed in the help_functions.php, updated the file and it worked as expected.
$images = array( "https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260", "https://images.pexels.com/photos/3787839/pexels-photo-3787839.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940", "https://images.pexels.com/photos/3769312/pexels-photo-3769312.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940")
$rand_img = array_rand($images, 1);
$image =$images[$rand_img];
Instead of hardcoding, I register a new custom widget by adding below code in the functions.php
register_sidebar(array(
'name' => esc_html__('Splash images Widget', 'wpresidence'),
'id' => 'splashimage-widget-area',
'description' => esc_html__('The splash image widget area', 'wpresidence'),
'before_widget' => ' ',
'after_widget' => ' ',
'before_title' => ' ',
'after_title' => ' ',
));
add_action( 'widgets_init', 'register_my_widgets' );
and I added the custom html to the widget and added the below code
array( "https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260", "https://images.pexels.com/photos/3787839/pexels-photo-3787839.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940", "https://images.pexels.com/photos/3769312/pexels-photo-3769312.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940")
And tried updating the code in help_functions.php file as below
$images = dynamic_sidebar( 'splashimage-widget-area' );
$rand_img = array_rand($images, 1);
$header_type=20;
$image =$images[$rand_img];
Its not working as expected.
Expected result, Add images link in the widget and update the help_functions.php file to show the different images on refresh.
I register a new custom widget
You are registering an area for the widgets using register_sidebar(), not the widget itself.
This widget area allows you to assign widgets (custom HTML, image, recent posts etc) into that area and specify where you want them to appear in your template files.
To register a widget you need to use register_widget() function and class, see this tutorial on how to create your first widget.
However, if you don't want to register new widget you can register shortcode that outputs what you want, then add it via a custom html/text widget.
Registering shortcode is very easy using add_shortcode() function, especially if you plan to hardcode an array of images and not pass any attributes.
More on registering shortcode here.

How to show different content in the sidebar on a static home page

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.

Missing Widgets in Wordpress dashboard?

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.

WordPress sidebar text widget

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.

Wordpress Navigation Help

I am working on a site that calls the categories of a wordpress page and displays them in the right-side navigation using a php call. I am new to php and web programming in general. Is there a way I could split the categories into two sections using a particular php call or perhaps an if-loop.
Essentially, I want to display particular categories under custom sub-headings to better organize the site. Any help, I'm currently using the following script to display my categories:
<ul><?php wp_list_categories('show_count=1&title_li='); ?></ul>
Here is my site for reference: http://www.merrimentdesign.com
Try using your code above twice. Each time, you can use the other function arguments to limit the output to certain categories. See http://codex.wordpress.org/Template_Tags/wp_list_categories for the various ways to customize the output of the function.
For example you could use:
<ul><?php wp_list_categories('show_count=1&title_li=&child_of=100'); ?></ul>
// where 100 is the parent id of all of the categories you want to print.
<ul><?php wp_list_categories('show_count=1&title_li=&exclude_tree=100'); ?></ul>
// and then show everything, but children of 100
Or simply use the first string multiple times specifying different parent ids each time.
By far and away your best option is to use the new menu functionality within WordPress. It's dead straight forward to set up in your theme:
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'public-menu' => __( 'Public Menu' ),
'sidebar-public-menu' => __( 'Sidebar Public Menu' ),
'sidebar-members-menu' => __( 'Sidebar Members Menu' ),
'sidebar-staff-menu' => __( 'Sidebar Staff Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
place that in your functions.php file (and obviously change it for your requirements).
Then in your template file - probably sidebar.php you'll want something like:
<?php wp_nav_menu( array( 'theme_location' => 'sidebar-staff-menu', 'container' => false ) ); ?>
And then go to the back end of WordPress (your wp-admin) and then go to Appearance > Menus and voila you're able to drag and drop your categories to your heart's content!
Helpful link: http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus
Read that, Justin Tadlock is awesome.
Good luck.

Categories