If statement to show dynamic_sidebar
-
Hi, I have question about this if statement
if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer'))
I know how IF works but I don’t know why in this case is !function_exist
I think if this function should appearance a Footer sidebar then it should looks like
if(function_exist('dynamic_sidebar) || dynamic_sidebar('Footer'))Could you tell me why in this case was used !function_exist OR !dynamic_sidebar?
And where can I find function_exist and dynamic_sidebar references?
-
What theme is this referring to? What’s the rest of the code following the if statement. The if statement here means “if the
dynamic_sidebar()function doesn’t exist[*] or if there are no widgets assigned to the footer area”, so it’s probably used to display some predefined content in case the user didn’t assign any widgets to the footer area.function_exists(): http://php.net/manual/en/function.function-exists.php
dynamic_sidebar(): https://codex.ww.wp.xz.cn/Function_Reference/dynamic_sidebar[*] The
dynamic_sidebar()function was introduced in version 2.2, which is 8 years old at this point. The likelihood of anyone still running version 2.2 on an actively-maintained site is extremely low.to follow-up on th ecorrect reply by @stephencottontail:
the check with
function_exists('dynamic_sidebar')was used in the transition time when the function was first introduced in wp2.2.0 – now, you can safely remove it, i.e. your posted code would simplify to:if( !dynamic_sidebar('Footer') )you have only posted part of the full code – please post the full code to get a full explanation.
review https://codex.ww.wp.xz.cn/Function_Reference/dynamic_sidebar#Examples
I do not know how to thank you 🙂
I understand now how it works.if(!dynamic_sidebar('footer') // if not exist dynamic sidebar named id footer (or something like that) // display static text endif;I changed this code to
<?php if ( is_active_sidebar('footer') ) : ?> //check if sidebar's active <?php dynamic_sidebar( 'footer' ); ?> //if yes display it <?php endif; ?>Thank you very much! 🙂
this single line would perfectly work on its own:
<?php dynamic_sidebar( 'footer' ); ?> //if yes display itthe check for an active sidebar is actually only necessary and useful if you want to wrap the sidebar code for example into some html tags;
like:
<?php if ( is_active_sidebar('footer') ) : ?> //check if sidebar's active <div class="footer-sidebar"> <?php dynamic_sidebar( 'footer' ); ?> //if yes display it </div> <?php endif; ?>You are very helpful,
Thank you so much 🙂
The topic ‘If statement to show dynamic_sidebar’ is closed to new replies.