0

So I have 4 widgets which is in large-3 columns, when they're not active I place a placeholder as user guide so the user would know where to put what.

I did manage to work out the is_active but it turns out to be a long code, I was wondering if there is a way to shorten it.

Widgets from available-course-list-1 to -4

<?php if ( is_active_sidebar( 'available-course-list-1' ) ) : ?>
    <div class="large-3 medium-3 columns">
     <div class="panel">
       <?php dynamic_sidebar( 'available-course-list-1' ); ?></div>
     </div>
<?php else: ?>
    <div class="large-3 medium-3 columns">
    <div class="panel">
    <h5>Available Course List #2</h5>
    <p>To add lists courses in this widget, please use the Available Course List widget panel.</p>
    </div>
    </div>
<?php endif; ?>

I tried something like this:

<?php if ( is_active_sidebar( 'available-course-list-1', 'available-course-list-2' ) ) : ?>
        <div class="large-3 medium-3 columns">
         <div class="panel">
           <?php dynamic_sidebar( 'available-course-list-1', 'available-course-list-2' ); ?></div>

but that didn't work.

2

is_active_sidebar has a single value, whether it is the sideabar id or name. If you have more than one sidebar, you should repeat is_active_sidebar, each one with a specific sidebar id or name.

You would do something like this

<?php if ( is_active_sidebar( 'available-course-list-1' ) && is_active_sidebar( 'available-course-list-2' )) : ?>
0
1

I've managed to find a slightly different solution, not sure if it 100% fits your problem, but hopefully it will help someone else out there. Place the following information in your sidebar.php file:

<?php
  $myCounter =  1;
  while (is_active_sidebar("services-" . $myCounter)) {
    echo '<aside id="secondary" class="widget-area" role="complementary">';
      dynamic_sidebar("services-" . $myCounter); 
    echo '</aside><!-- #secondary -->';
  $myCounter++;
  }
?>

Code Explanation: (Line numbers on the left)

  1. Declare variable and initialize to 1
  2. While there is a sidebar with published content in the sidebar named (in the first iteration of the while loop) "services-1"
  3. Place within aside tag with the id #secondary and and the class .widget-area where the role is complementary.
  4. Insert the dynamic sidebar called services-1 (in the first iteration of the while loop)
  5. Close the aside tag.
  6. Add 1 to the variable $myCounter (iterate by 1)
  7. End While Loop

Remember that you need to add theme support in your functions.php file (credit here):

add_action( 'after_setup_theme', 'ja_theme_setup' );
function ja_theme_setup() {
    add_theme_support( 'post-thumbnails');
}

as well as adding the following code to where you want your sidebar to appear, for example, in your front-page.php page for example:

get_sidebar('services-1');

I think this example is great if you would like to just copy and paste the code and alter the services- name.

You can then style the sidebar by using echo and placing other HTML elements around it and using CSS in your style.css file.

[edit: 07.05.2017 21h46]

The only issue with the above answer is that if one of the widgets in between aren't being used, the iteration stops there, and you wont get the rest of the sidebars/widgets.

In my case, I know I used 5 sidebars, so I created the following code instead of the while loop:

$myCounter = 1;
for ($myCounter = 1;$myCounter < 6;$myCounter++) {
if (is_active_sidebar("services-" . $myCounter)) {
    echo '<aside id="secondary" class="widget-area" role="complementary">';
    dynamic_sidebar("services-" . $myCounter); 
    echo '</aside><!-- #secondary -->';
}   

}

0

Well, the very obvious way to shorten it is to simply avoid repeating yourself:

?>
<div class="large-3 medium-3 columns">
  <div class="panel"><?php
    if ( is_active_sidebar( 'available-course-list-1' ) ) {
      dynamic_sidebar( 'available-course-list-1' );
    } else { ?>
      <h5>Available Course List #2</h5>
      <p>To add lists courses in this widget, please use the Available Course List widget panel.</p><?php
    } ?>
  </div>
</div><?php

Neither is_active_sidebar() nor dynamic_sidebar() accept "lists" of sidebars, so what you are trying isn't going to work. There is no Core functionality that I know of to simplify your code, just basic PHP and some thought.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.