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

Tiêu đề widget

 
- Cho phép hiển thị HTML cho tiêu đề widget:
remove_filter( 'widget_title', 'esc_html' );
 
- Sửa tiêu đề widget bạn có thể chỉ định vào widget bởi tham số $id_base được truyền vào hàm filter widget_title. VÍ dụ sau tôi lấy tiêu đề của trang bài viết hiện tại làm tiêu đề widget.
function myplugin_widget_title( $title, $instance, $id_base ) {
    if ( !is_single() ) {
        return $title;
    }
 
    $post_title = get_the_title();
    switch ( $id_base ) {
        case 'pages': return sprintf( '%s "%s"', $title, $post_title );
        case 'links': return sprintf( 'Links for "%s" post.', $post_title );
        // other widgets ...
        default: return $title;
    }
}
add_filter( 'widget_title', 'myplugin_widget_title', 10, 3 );
 
- Tạo link cho tiêu đề widget:
Ý tưởng là tạo biến toàn cục lưu trữ các liên kết của widgets, tiếp đó sử dụng hook 'widget_title' chúng ta sẽ thiết lập liên kết dựa vào tiêu đề có lưu ở biến toàn cục đó.
add_action('init','setup');
function setup(){
     global $w_title_links;
     $w_title_links=array(
          'danh-sach-san-pham'=>site_url('san-pham'),          //for home widget
          'ban-chay-nhat'=>site_url('ban-chay'),     //for home widget
          'ban-chay-nhayt'=>site_url('ban-chay'),     //for home widget
          'ban-chay'=>site_url('ban-chay'),     //for home widget
          'giam-gia'=>site_url('giam-gia'),
          'gio-hang'=>site_url('gio-hang'),
          'gia-hang'=>site_url('gio-hang')
     );
}
 
Sửa tiêu đề widget với filter 'widget_title'.
/*
* note: we cover widget title on hook 'dynamic_sidebar_params' but this hook may be work with together, because 'dynamic_sidebar_params' can't change widget title
*/
add_filter('widget_title','widget_title_link');
function widget_title_link($title){
     global $w_title_links;
     $not_in = array('danh-sach-san-pham','ban-chay-nhat','ban-chay-nhayt');          #don't apply for those
     $san_title = sanitize_title(strtolower($title));
     if(!in_array($san_title,$not_in) && in_array($san_title, array_keys($w_title_links) )) {
          preg_match('/href=(\'|")(.+?)(\'|")/',$title,$s);
          if(isset($s[2])) {
               $title=str_replace($s[2],$w_title_links[$san_title],$title);
          }
          else $title = '<a href="'.$w_title_links[$san_title].'">'.$title.'</a>';
     }
     return $title;
}
 
Ngoài ra trong khi gọi sidebar các widget cũng bắt đầu nạp bạn có thể sử dụng filter 'dynamic_sidebar_params' để thêm link cho tiêu đề widget.
add_filter('dynamic_sidebar_params','widget_first_last_classes');
function widget_first_last_classes($params){
        ...
        /*make anchor for what widget title go to their page*/
     global $w_title_links;
     $san_title = sanitize_title(strtolower($widget_opt[$widget_num]['title']));
    if(in_array($san_title, array_keys($w_title_links))){     //modify if exists anchor link on widget title
          if(strpos($params[0]['before_title'],'<a')!==false){
               preg_match('/href=(\'|")(.+?)(\'|")/',$params[0]['before_title'],$s);
               if(isset($s[2])) {
                    $params[0]['before_title']=str_replace($s[2],$w_title_links[$san_title],$params[0]['before_title']);
               }
               else $params[0]['before_title'] = str_replace('<a','<a href="'.$w_title_links[$san_title].'"',$params[0]['before_title']) ;
          }
     }
}
 
 
Made with help of Dr.Explain

Unregistered version