0

I have created some widgets for my WordPress site. I use fallback data widget WordPress which I got form this free WordPress theme: https://wordpress.org/themes/brightpage.

So, if I don't put any content in my widget, then show the default content. But when I put anything into the widget like text or image, then default content would be disappear and the content which I just put into the widget would be appear.

But when I put something in Widget such as text or image it not change dynamically form the widget area in WordPress admin panel.

I want to share at least two examples from my WordPress site. This code form header.php:

<header class="fix header_top_area">
<div class="fix container header_top">
 <?php if ( ! dynamic_sidebar( 'header-top' ) ) : ?>

    <div class="fix floatright header_top_text">
         <div class="fix floatleft call">
            <p>Chicago, USA,  Call : 00 000 0000</p>
         </div>
         <div class="fix floatleft sign_in">
            <a href="">Sign In</a>
        </div>
    </div>
   <?php endif; ?>
  </div>
  </header>

  <header class="fix header_bottom_area">
  <?php if ( ! dynamic_sidebar( 'header-bottom' ) ) : ?>
    <div class="fix container header_bottom">
    <figure class="fix floatleft logo">
          <a href="<?php bloginfo('home'); ?>"><img src="<?php bloginfo('template_url');?>/img/logo.png" alt="" /></a>
    </figure>
    <figure class="fix floatright image_header">
         <a href=""><img src="<?php bloginfo('template_url');?>/img/image_header.jpg" alt="" /></a>
    </figure>
</div>
<?php endif; ?>

This code form functions.php:

add_action('widgets_init', 'wp_widget_areas');
function wp_widget_areas() {
register_sidebar( array(
    'name' => __( 'Home Page Widget Header Top', 'Perfect Point' ),
    'id' => 'header-top',
    'before_widget' => '<div class="fix floatright header_top_text">',
    'after_widget' => '</div>',
    'before_title' => '<h2 style="display:none">',
    'after_title' => '</h2>',

) );

register_sidebar( array(
    'name' => __( 'Home Page Widget Header Bottom', 'Perfect Point' ),
    'id' => 'header-bottom',
    'before_widget' => '<div class="fix container header_bottom">',
    'after_widget' => '</div>',
    'before_title' => '<h2 style="display:none">',
    'after_title' => '</h2>',

) );

}

How could I active these widgets? Can anybody help me to fix it? Thanks.

1 Answer 1

0

If I understand correctly, the issue you're facing is that the widgets aren't showing up when they're populated.

This is caused by the php if statement you're using not containing an else argument. If you change your code to look like below, it should work.

<header class="fix header_top_area">
    <div class="fix container header_top">

        <?php if (!dynamic_sidebar('header_top')) : ?>

        <div class="fix floatright header_top_text">
            <div class="fix floatleft call">
                <p>Chicago, USA,  Call : 00 000 0000</p>
            </div>

            <div class="fix floatleft sign_in">
                <a href="">Sign In</a>
            </div>
        </div>

        <?php
            else :
                dynamic_sidebar('header_top');

            endif; 
        ?>

    </div>
</header>

<header class="fix header_bottom_area">
    <?php if (!dynamic_sidebar('header_bottom')) : ?>

    <div class="fix container header_bottom">
        <figure class="fix floatleft logo">
            <a href="<?php bloginfo('home'); ?>">
                <img src="<?php bloginfo('template_url');?>/img/logo.png" alt="" />
            </a>
        </figure>

        <figure class="fix floatright image_header">
            <a href="">
                <img src="<?php bloginfo('template_url');?>/img/image_header.jpg" alt="" />
            </a>
        </figure>
    </div>

    <?php
        else :
            dynamic_sidebar('header_bottom');

        endif; 
    ?>

The issue with the code you were using was that it doesn't know what to do is the sidebar isn't empty. With the code above, when the sidebar is populated, it will show that content.

Let me know if that doesn't work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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