Dynamic Sidebars & Echo

I can sucesfully get my dynamic sidebar to work like so:

<div class="sidebar sidebar-first">
    <ul class="widget-area first">
        <?php dynamic_sidebar( 'pop-photos-sidebar' ); ?>
    </ul>
</div>

Now, I’m trying to get the sidebar to work with a different code that is inside a php code. I tried using the above < ?php dynamic_sidebar( 'pop-photos-sidebar' ); ? > code and it broke my site since the php dynamic_sidebar code is inside another php code. So I tried making a $ function like so:

Read More
$newssidebar = dynamic_sidebar( 'pop-news-sidebar' );
            echo '<div class="sidebar sidebar-first">
                <ul class="widget-area first">
                '.$newssidebar.'
                 </ul>
             </div>';

This sucesfully displayed the dynamic side bar “pop-news-sidebar” but.. it is not inside the ul class widget-area first, or inside the div class sidebar sidebar-first. How would I make my dynamic sidebar to properly be displayed in the ul class and div class listed above like it should be? Would I even use echo? I didn’t think it was necessary to paste the full code, but here it is anyways:

<?php
    if( fake if code ) {
        if( another fake if code ) {
            $newssidebar = dynamic_sidebar( 'pop-news-sidebar' );
            echo '<div class="sidebar sidebar-first">
                <ul class="widget-area first">
                '.$newssidebar.'
                </ul>
            </div>';
        }else if( fake else ) {
            echo 'some other code';
         }
    }

?>

Any help would tremendously be appreciated! 🙂

Related posts

Leave a Reply

1 comment

  1. You don’t need to echo dynamic_sidebar(). End your echo, call dynamic_sidebar(), and then start a new echo. To make it easier, just close/reopen your PHP tags:

    <?php
    if( fake if code ) {
        if( another fake if code ) {
            ?>
            <div class="sidebar sidebar-first">
                <ul class="widget-area first">
                    <?php dynamic_sidebar( 'pop-news-sidebar' ); ?>
                </ul>
            </div>
            <?php
        } else if( fake else ) {
            echo 'some other code';
        }
    }    
    ?>