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 hiển thị widget

Hiển thị widgets cho từng trang trong wordpress:
Bạn đã biết cách tùy biến sidebar hiển thị widgets trong template wordpress. Đa phần nhiều website có thiết kế cố định các mục widgets ở sidebar và sử dụng một sidebar duy nhất. Nhưng nếu bạn muốn hiển thị các sidebar widgets khác nhau trong từng trang cụ thể thì làm thế nào?
 
Hiển thị widget chỉ định vào sidebar:
Tạo hàm kiểm tra sự cho phép hiển thị widget ở sidebar nào.
function jml_sidebar_has_widget( $widget_id, $sidebar ) {
 
    $sidebars = get_option( 'sidebars_widgets' );
 
    if ( ! array_key_exists( $sidebar, $sidebars ) )
        return false;
 
    if ( ! in_array( $widget_id, $sidebars["$sidebar"] ) )
        return false;
 
    return true;
}
 
Kiểm tra nếu widget có đặt vào đúng sidebar không.
/**
* Display the widget
*
* @param array $args Widget display arguments including
* before_title, after_title, before_widget, and
* after_widget.
*
* @param array $instance The settings instance of the widget
*/
public function widget( $args, $instance ) {
    extract( $args );
 
    $title = apply_filters('widget_title', $instance['title'] );
 
    $current_widget_id = "{$this->id_base}-{$this->number}";
 
    // Check if this widget is placed in `sidebar-2`
    if ( jml_sidebar_has_widget( $current_widget_id, 'sidebar-2' ) ) : ?>
 
        <!-- Display the widget here -->
 
    <?php else : ?>
 
        // Widget is in the wrong place. Tell the user.
        <?php if ( current_user_can( 'edit_theme_options' ) : ?>
 
            <?php _e( 'This widget is designed to work only in Content Sections widget area.', 'jml' ); ?>
 
        <?php endif; ?>
 
    <?php endif; ?>
}
 
 
Made with help of Dr.Explain

Unregistered version