Home WordpressWordPress cơ bản WordPress theme swicher without plugin

WordPress theme swicher without plugin

by admincp

Chuyển đổi theme phù hợp tương thích với từng thiết bị (desktop,mobile,tablet) hay thiết lập sang theme mới (switch new theme) không dùng plugin có 2 cách bạn active sang theme mới hoặc chuyển thư mục theme khác để sử dụng.

Cách 1: Active theme

/*set active theme: thiết lập active sang theme mới*/
switch_theme('nop-ho-so-yellow');		//folder theme

Bạn có thể thao tác trong admin, để thay đổi theme làm tương tự truy cập vào Appearance->themes, chọn theme cần thay đổi và nhấn active.
Ý tưởng switch theme được nhắc đến trong khi bạn thiết kế giao diện theme cho từng loại thiết bị, về kích thước Viewport.
theme mobile viewport - hoangweb.com

Hiểu về switch theme?

Khi duyệt web có một đoạn code nhận dạng về thông tin trình duyệt từ đó xác định thiết bị đang dùng lướt web. Dựa vào đó để cài đặt theme: với hình thức vẫn sử dụng cấu trúc files của theme, nhưng chuyển thư mục là thư mục của theme khác (được chỉ định thay đổi) bởi hàm get_template_directory()

ie: get_template_directory();	#là folder của theme khác

Chú ý: cấu trúc files template của theme cần giống nhau chỉ thay đổi nội dung.
THam khảo 2 cách sau:

Cách 1: áp dụng filter pre_option_stylesheet

//method 1
add_filter( 'pre_option_stylesheet', 'wpse_100854_switch_stylesheet' );
 
function wpse_100854_switch_stylesheet( $false )
{
    if ( empty ( $_GET['theme'] )
        return $false;
 
    $themes = wp_get_themes( array( 'allowed' => true ) );
 
    if ( isset ( $themes[ $_GET['theme'] ] ) )
        return $_GET['theme'];
 
    return $false;
}

Cách 2: kết hợp các filter hook template.

//method 2
add_filter('template', 'serve_default_to_iesix');
add_filter('option_template', 'serve_default_to_iesix');
add_filter('option_stylesheet', 'serve_default_to_iesix');
 
function serve_default_to_iesix($theme) {
    //if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false)
        $theme = 'nop-ho-so-yellow';
    return $theme;
}

Nếu bạn thấy bài viết này hữu ích, hãy chia sẻ với bạn bè bằng cách nhấn nút chia sẻ ở bên dưới. Theo dõi chúng tôi trên TwitterFacebook

Liên hệ

Công ty chuyên Thiết kế website uy tín nhất Miền Bắc: http://vinastar.net

Hotline tư vấn: 0989 48 3456

Nguồn: Sưu tầm trên internet

You may also like

Leave a Comment