0

I'm implementing some widget area inside my custom theme. I've noticed that the added widgets are not display where I want, It will appear a blank area, also if I create a custom sidebar template for that. Is possible to check if there are widgets active for the theme and how? How I can fix the problem I'm facing?Here is the code I'm using:

functions.php

  function contact_widget()
  {
    register_sidebar(
      array(
        'name'          => __('Contact'),
        'id'            => 'contact-info',
        'description'   => __('Contact info'),
        'before_widget' => '<div class="col-sm-12 col-md-6 col-lg-6 contact-widget>',
        'after_widget'  => '</div>',
        'before_title'  => '<h1>',
        'after_title'   => '</h1>',
      )
    );
  }
  add_action( 'widgets_init', 'contact_widget' );

  function social_widget()
  {
    register_sidebar(
      array(
        'name'          => __('Social'),
        'id'            => 'social-links',
        'description'   => __('Link social'),
        'before_widget' => '<div class="col-sm-12 col-md-6 col-lg-6 text-center>',
        'after_widget'  => '</div>',
        'before_title'  => '<h1>',
        'after_title'   => '</h1>',
      )
    );
  }
  add_action( 'widgets_init', 'social_widget' );

Inside the customizer it will appear only the first widget I've registered but not the second. In the part of the theme where I want to display them will not appear nothing instead. Yhis is the relative code.

<?php if( function_exists('register_sidebar') ): ?>
<div class="jumbotron jumbotron-fluid services-action">
  <div class="container">
    <div class="row">
<?php if( is_active_sidebar('contact-info') ): ?>
<?php  get_sidebar('contact-info'); ?>
<?php endif; ?>
<?php if( is_active_sidebar('social-links') ): ?>
<?php  get_sidebar('social-links'); ?>
<?php endif; ?>
    </div>
  </div>
</div>
<?php endif; ?>

I want that this part is showed only if the widgets are active.

The two templates for the sidebar will give a deprecation error if I don't create also the sidebar.php file. They only contain for now this line of code:

// sidebar-social.php
<?php dynamic_sidebar('social-info'); ?>
// sidebar-contact.php
<?php dynamic_sidebar('contact-info'); ?>
// sidebar.php
<?php get_sidebar(); ?>
```
Since the needed div classes are declared inside the registration of the sidebars I've not implemented nothing as html markup inside the templates files. If is possibile I will prefere to implement the div columns inside the sidebar template files instead using the `before_widget` and `after_widget`.
Any help will be appreciated.

0

Try to combine both sidebars/arrays into one function.

function my_widgets() {
    register_sidebar(
       array(
       'id' => 'contact-info',
       // add the rest here
       ),
    register_sidebar(
       array(
       'id' => 'social-links',
       // add the rest here
       ))
    );
}
add_action('widgets_init', 'my_widgets');

Then you call the deisred sidebar

<?php if(is_active_sidebar('contact-info')) : ?>
   <?php dynamic_sidebar('contact-info'); ?>
<?php endif; ?>
  • I discovered that in the widget dashboard of wp the second sidebar area remain collapsed. I think this is because I'm using adblock plus. Now I need to implement a custom widget to output what I want without any wordpress additional div or css class! – sialfa Dec 17 '19 at 15:49

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.