0

I'm trying include dynamic sidebar to Uncode Theme. I created child template, then I created all files and now I have a piece of code who shows content and sidebar area.

In functions.php I created dynamic sidebar:

add_action( 'widgets_init', function () {
    $args = array(
        'name'          => __( 'Sidebar Messages Single', 'uncode' ),
        'id'            => 'sidebar-messages-single', 
    'description'   => '',
        'class'         => 'dynamic-widget',
    'before_widget' => '<li id="dynamic-sidebar" class="widget">',
    'after_widget'  => '</li>',
    'before_title'  => '<h2 class="widgettitle">',
    'after_title'   => '</h2>'
    ); 

    register_sidebar( $args );
});

Then in single-portfolio.php I'm trying include that sidebar.


$sidebar_widgets = is_active_sidebar( 'sidebar-messages-single' ) ? dynamic_sidebar( 'sidebar-messages-single' ) : null;

$the_content = '<div class="row-container">
    <div class="row row-parent' . $row_classes . $limit_content_width . '">
        <div class="row-inner">
            ' . (($layout === 'sidebar_right') ? $main_content : '') . '
            <div class="col-lg-' . $sidebar_size . '">
                <div class="uncol style-' . $portfolio_style . $expand_col . $sidebar_padding . (($sidebar_fill === 'on' && $portfolio_bg_color !== '') ? '' : $sidebar_sticky) . '">
                    <div class="uncoltable' . (($sidebar_fill === 'on' && $portfolio_bg_color !== '') ? $sidebar_sticky : '') . '">
                        <div class="uncell' . $sidebar_inner_padding . '">
                            <div class="uncont">
                                <div>
                                    ' . $sidebar_widgets . '
                                </div>
                                ' . $footer_content . '
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            ' . (($layout === 'sidebar_left') ? $main_content : '') . '
        </div>
    </div>
</div>';

It seems to me everything is alright, but when I render page I get this result.

enter image description here The place where sidebar should be render I get int (one). Whereas sidebar is rendered in another place.

At first I thought it is DOM manipulated via JavaScript, but when I debugged and disabled JS it worked the same.

Furthermore, when I added normal string to <div> beside variable $sidebar_widget. String rendered in correct place.

Thanks a lot for help!

  • 1
    You can't put dynamic_sidebar() into a variable like that. That function echoes its results. Is there a particular reason you've built your template as a giant string? – Jacob Peattie Aug 13 at 6:51
  • Unfortunately, this is theme providers work (Uncode). I try to add function directly to string but I get the same result. I want to avoid reconstruction all theme for only add sidebar. – Aaron Nusselbaum Aug 13 at 6:56
  • actually @Jacob Peattie is completely right, you can't store dynamic_sidebar cannot be saved in a variable, because it always echoes out the widget, you have to add it directly within your markup – Makiomar Aug 13 at 8:35
1

You shouldn't save dynamic_sidebar in a variable because it always echoes out the widget. the following should work.

<?php

echo '<div class="row-container">
<div class="row row-parent' . $row_classes . $limit_content_width . '">
    <div class="row-inner">';
     if(($layout === 'sidebar_right') && is_active_sidebar( 'sidebar-messages-single' )) {
        dynamic_sidebar( 'sidebar-messages-single' );
     }
      echo '<div class="col-lg-' . $sidebar_size . '">
            <div class="uncol style-' . $portfolio_style . $expand_col . $sidebar_padding . (($sidebar_fill === 'on' && $portfolio_bg_color !== '') ? '' : $sidebar_sticky) . '">
                <div class="uncoltable' . (($sidebar_fill === 'on' && $portfolio_bg_color !== '') ? $sidebar_sticky : '') . '">
                    <div class="uncell' . $sidebar_inner_padding . '">
                        <div class="uncont">
                            <div>';
                                 if(is_active_sidebar( 'sidebar-messages-single' )) {
                                     dynamic_sidebar( 'sidebar-messages-single' );
                                 }
                               echo '</div>
                            ' . $footer_content . '
                        </div>
                    </div>
                </div>
            </div>
        </div>';
         if(($layout === 'sidebar_left') && is_active_sidebar( 'sidebar-messages-single' )) {
            dynamic_sidebar( 'sidebar-messages-single' );
         }
    echo '</div>
</div>
</div>';
  • Thank you. It is the correct way. But my theme provider processes $the_content and I can't show the result at this moment. I contact with them. – Aaron Nusselbaum Aug 13 at 15:34
1

Solution for someone have to use variable

Thanks to the topic how do I get_sidebar into a varaible?

I change my code into:

...
<div class="uncont">
ob_start();
if(is_active_sidebar( 'sidebar-messages-single' )) {
    dynamic_sidebar( 'sidebar-messages-single' );
}
$the_content .= ob_get_contents();
ob_end_clean();
</div>
...

I know that isn't it elegant solution, but if someone has to fast fix 3rd party theme (eg. Uncode) it is one of the ways.

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.