Forums

In Template - Alternate sidebar if no widgets assigned (3 posts)

  1. Zentode
    Member
    Posted 2 years ago #

    I would like to have a "switch" in my template file that "reports" whether a widget exists in a dynamic sidebar so I can include an alternate sidebar file if no widgets have been assigned.

    Basically, if there were no widgets dragged over in the Appearance / Widgets area, my alternate sidebar file will be used.

    In this section of my template:

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('left-side-widgets') ) : ?>

    I would like to add something like:

    <?php elseif no widgets : ?>
    <?php /* include alternate sidebar file in dynamic sidebar */ ?>
    <?php endif; ?>`

    How can I create that "switch"?

    Is it possible? Yes. By me, no... I tried. :-/

    How do I create that query to the db to return the "switch"?

    Additionally, how about also returning how many widgets were dragged over there?

    And world peace.

    I can use:
    file_get_contents($someurl);
    to grab the file if I must.

    Thanks for any help...

  2. doodlebee
    Member
    Posted 2 years ago #

    A working example of *exactly* what you're looking for is included in the default theme's sidebar.php file.

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('left-side-widgets') ) : ?>
    
    put your default "show this stuff if no widgets are activated" stuff here
    
    <?php endif; ?>

    No, I see you've named a sidebar widget section - so if you want stuff to show up on all other pages that *aren't* using "left-side-widgets" then just edit that a bit:

    <?php if( !dynamic_sidebar('left-side-widgets') ) { ?>
     put the stuff for a sidebar if the page isn't using the left side widgets
    <?php } else { ?>
    
    display the default stuff that shows if you're on a page that uses left-sidebar-widgets, but no widgets are pulled in yet
    
    <?php } ?>
  3. Zentode
    Member
    Posted 2 years ago #

    Thank you,

    I ended up using the "If no left side widgets, display "this"" approach you pointed out.

    <?php if( !dynamic_sidebar('left-side-widgets') ) : ?>
    Show this file if no widgets
    <?php endif; ?>

    is what I ended up with.

    I must now attempt to query how many widgets, (the wp rss feed widgets specifically) are being displayed and their settings.
    I will do some reading here on the best way to query the db from a template file.

    Thanks again..

Topic Closed

This topic has been closed to new replies.

About this Topic