Site logo

Archived topic

dynamic_sidebar not working!!

4 replies · Started by Mohamed on November 16, 2020

Viewing posts 1–5 of 5

Hi,

I had created a custom dynamic sidebar with this code but not working!!


register_taxonomy( 'product_cat', array('product'), array() );

$terms = get_terms( array ( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'depth' => 0,'exclude'=> 15,'parent' => 0, 'orderby' => 'description', 'order' => 'ASC' ));

$product_categories = [];
    foreach ($terms as $k => $term) {
       $product_categories[$k]=  $term->name ;
    }
        
        function custom_sidebars() {
            
            if(!is_admin()) return;
            global $product_categories,$terms;
        Null !==($product_categories) ? $sidebars = ($product_categories) : $sidebars ='';

$product_categories = [];
    foreach ($terms as $k => $term) {
       $product_categories[$k]['name']=  $term->name ;
       $product_categories[$k]['id']=  $term->term_id ;
    }

            if((isset($product_categories) ) && (!empty($product_categories) ))
                foreach ($product_categories as $i => $sidebar) {
                //$sidebar_name = preg_replace('/\s+/', '_', $sidebar);
                    register_sidebar ( array(
                    'name' => $sidebar['name'],
                    'id'   => 'net_pb_widget_area_'.$sidebar['id'],  
                    'description' => __( 'net_pb_widget_area_'.$sidebar['id'] . '[products limit="3" columns="1" best_selling="true" category="'.$sidebar['id'].'" ]','ar2' ),
                    'before_widget' => '<div id="%1$s" class="widget %2$s">',
                    'after_widget' => '</div>',
                    'before_title' => '<h3 class="widget-title"><span>',
                    'after_title' => '</span></h3>'
                    ));
                    
                }
            
            
        }
        add_action( 'widgets_init', 'custom_sidebars' );
        add_action("admin_init", "sidebar_init");
        add_action('save_post', 'save_sidebar_link');
        function sidebar_init(){
            add_meta_box("sidebar_meta", "Sidebar Selection", "sidebar_link", array("post","page","product"), "side", "default");
        }
        
        function sidebar_link(){
            
                        if(!is_admin()) return;

            global $post, $sidebar,$product_categories;
            $custom  = get_post_custom($post->ID);
             $theSidebar  = get_post_meta($post->ID,'sidebar',true);
            if ( isset($theSidebar))
            $link    = $theSidebar;
            
            
            $sidebars = $product_categories ;

                    
            echo '<div class="link_header">';
            echo '<select name="link" class="sidebar-selection">';
            echo '<option value="primary-sidebar" style="font-weight:bold;">Default Sidebar</option>';
            if (!empty($sidebars)) {
                foreach ( $sidebars as $id => $list ){

                    if($link == 'net_pb_widget_area_'.$list["id"]){
                        echo '<option value="net_pb_widget_area_'.$list["id"].'" selected="true">'.$list['name'].'</option>';
                    }else{
                        echo '<option value="net_pb_widget_area_'.$list["id"].'">'.$list['name'].'</option>';
                    }
                }
            }
            echo '</select>';
            echo '<br /></div><p>Select sidebar to use on this page.</p>';
            
        }
        function save_sidebar_link(){
                        if(!is_admin()) return;

            global $post;
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {return $post->ID;}
            if ( isset( $_POST["link"] ) )
            update_post_meta($post->ID, "sidebar", $_POST["link"]);
        }
        add_action('admin_head', 'sidebar_css');
        function sidebar_css() {
            echo'
                        <style type="text/css">
                        .sidebar-selection{width:100%;}
                        </style>
                        ';
        }

and using this code to display it but it's empty


 global $post;
     $postID= $post->ID;
     $sidebar  = get_post_meta($postID,'sidebar',true);
        if(in_array(get_post_type(), array('post','page'))){
        
                       if ('' != $sidebar ){
        		echo  '<div class="theSide">';	    
                       dynamic_sidebar('$sidebar ');
                       echo '</div>';   
                       }

Could you help?

Hi,

I don't see any function dynamic_sidebar(){} within your code for this dynamic_sidebar('$sidebar '); to do anything.

My bad. misread it a bit. It's a core function.

Try changing dynamic_sidebar('$sidebar '); to dynamic_sidebar($sidebar);.

Adding '' makes whatever is enclosed within it, a string.

Since $sidebar is a variable, you don't have to add ''.

My question because it was working before so, why it doesn't now?

A wise man always said
" Yes I already cleared the cache"

Could you tell me why it doesn't working?

Bear with me as this is going to be pretty lengthy.

For starters, did you change anything within the code before it stopped working?

I believe you've added this to your functions.php and like most users, at some point accidentally removes/adds syntaxes that breaks the code.

Example:

 global $post;
     $postID= $post->ID;
     $sidebar  = get_post_meta($postID,'sidebar',true);
        if(in_array(get_post_type(), array('post','page'))){
        
                       if ('' != $sidebar ){
        		echo  '<div class="theSide">';	    
                       dynamic_sidebar('$sidebar ');
                       echo '</div>';   
                       }

This particular code of yours has few issues.

One is that it's missing a closing } for the if(in_array(get_post_type(), array('post','page'))) condition.

Another one is, as mentioned previously, dynamic_sidebar('$sidebar '); is taking in a text literal string "$sidebar" rather than taking the variable value of $sidebar because of the '' that encloses it. To be honest, I find it weird that this worked at all because of this.

This archived topic is closed to new replies.