TODO: To change the header's content go to Dr.Explain menu Options : Project Settings : HTML (CHM) Export : Setup HTML Template and Layout
×
Menu
Index

Tùy biến widget sidebar

Bằng cách sử dụng hook 'dynamic_sidebar_params' cho phép bạn tùy chỉnh tham số sidebar mà trước đó bạn có đăng ký bởi hàm register_sidebar.
Ví dụ Thêm class vào widget trong wordpress, ví dụ mình thêm vào widget thuộc sidebar footer.
/* Add dynamic_sidebar_params filter */
add_filter('dynamic_sidebar_params','footer_widgets');
 
/* Register our callback function */
function footer_widgets($params) {     
 
     global $footer_widget_num; //Our widget counter variable
 
     //Check if we are displaying "Footer Sidebar"
      if(isset($params[0]['id']) && $params[0]['id'] == 'footer_sidebar'){
         $footer_widget_num++;
      $divider = 3; //This is number of widgets that should fit in one row         
 
         //If it's third widget, add last class to it
         if($footer_widget_num % $divider == 0){
         $class = 'class="last ';
         $params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']);
      }
 
     }
 
      return $params;
}
 
Mọi thời điểm theme của bạn gọi hàm dynamic_sidebar, nó sẽ hiển thị tất cả widgets đã gắn vào sidebar đó. Wordpress sử dụng filter 'dynamic_sidebar_params' trước khi mọi widget được hiển thị vì vậy bạn có thể sử dụng hook này để sửa mọi widget theo ý muốn.
 
* Một ví dụ khác chúng ta class vào widget và đánh chỉ số, mỗi widget bắt đầu bằng ‘widget-n’, với n là chỉ số chạy từ 1 và đánh dấu class ‘widget-first’ cho widget đầu tiên, class ‘widget-last’ cho widget sau cùng.
/**
* Add "first" and "last" CSS classes to dynamic sidebar widgets. Also adds numeric index class for each widget (widget-1, widget-2, etc.)
*/
function widget_first_last_classes($params) {
 
    global $my_widget_num; // Global a counter array
    $this_id = $params[0]['id']; // Get the id for the current sidebar we're processing
    $arr_registered_widgets = wp_get_sidebars_widgets(); // Get an array of ALL registered widgets
 
    if(!$my_widget_num) {// If the counter array doesn't exist, create it
        $my_widget_num = array();
    }
 
    if(!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) { // Check if the current sidebar has no widgets
        return $params; // No widgets in this sidebar... bail early.
    }
 
    if(isset($my_widget_num[$this_id])) { // See if the counter array has an entry for this sidebar
        $my_widget_num[$this_id] ++;
    } else { // If not, create it starting with 1
        $my_widget_num[$this_id] = 1;
    }
 
    $class = 'class="widget-' . $my_widget_num[$this_id] . ' '; // Add a widget number class for additional styling options
 
    if($my_widget_num[$this_id] == 1) { // If this is the first widget
        $class .= 'widget-first ';
    } elseif($my_widget_num[$this_id] == count($arr_registered_widgets[$this_id])) { // If this is the last widget
        $class .= 'widget-last ';
    }
 
    $params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']); // Insert our new classes into "before widget"
 
    return $params;
 
}
add_filter('dynamic_sidebar_params','widget_first_last_classes');
 
Kết quả:
 
- Truy xuất đối tượng & dữ liệu widget:
Tất cả dữ liệu widgets được lưu trong biến $wp_registered_widgets. Nếu bạn sử dụng nhiều widget cùng loại thì mỗi dữ liệu options của widget đó được liệt kê theo chỉ số widget đó.
function hw_dynamic_sidebar_params($params){
    global $wp_registered_widgets;
    $widget_id = $params[0]['widget_id'];
    $widget_obj = $wp_registered_widgets[$widget_id];
    $widget_opt = get_option($widget_obj['callback'][0]->option_name);
    ...
    return $params;
}
add_filter('dynamic_sidebar_params','hw_dynamic_sidebar_params');
 
Giải thích:
     * $params[0]['widget_id']: widget ID.
     * $params[0]['id']: sidebar ID.
 
Đối tượng $widget_obj sẽ chứa giá trị mảng dạng như sau, ví dụ:
Array
(
    [name] => WP-dTree Categories
    [id] => wpdt-categories-widget-2  == $widget_id
    [callback] => Array
        (
            [0] => ...
 
            [1] => display_callback
        )
 
    [params] => Array
        (
            [0] => Array
                (
                    [number] => 2
                )
 
        )
 
    [classname] => wpdt-categories
    [description] => dTree navigation for your categories.
)
 
Chỉ số widget cùng loại, lưu trong đối tượng widget $widget_obj bởi thuộc tính number.
$widget_num = $widget_obj['params'][0]['number'];
 
Ok, bây giờ bạn có thể truy cập giá trị options của widget.
$opt1 = $widget_opt[$widget_num]['opion1'];
 
 
Made with help of Dr.Explain

Unregistered version