如何在 WordPress 中以编程方式设置主题?

Pri*_*ank 3 php wordpress wordpress-theming

我遇到的情况是,我在我的 php 网站中使用了多个主题,并且还集成了一个 wordpress 博客。

例如,这是我的网站网址: http://example.com

在那里我想通过传递一个查询参数来切换主题,例如:

http://example.com?mytheme=red_theme
http://example.com?mytheme=blue_theme
etc.
Run Code Online (Sandbox Code Playgroud)

目前我在 WordPress 中激活的主题是这样的blue_theme,我的 WordPress 博客 URL 是这样的:

http://example.com/blog?mytheme=red_theme
Run Code Online (Sandbox Code Playgroud)

例如:red_theme应该像预览一样显示。

否则,如果我通过这个 URL:

http://example.com/blog
Run Code Online (Sandbox Code Playgroud)

然后blue_theme应该显示默认主题 ( )。

我可以在核心 PHP 中调整它,但我不知道如何使用 WordPress。

小智 5

在 WORDPRESS 中,您可以根据设备以编程方式设置主题,例如移动设备上的不同主题和桌面上的不同主题。在您的默认主题的functions.php 中编写以下代码

function use_mobile_theme() {
    // Chech device is mobile or not
    if(wp_is_mobile()){
        return 'theme19388'; // set theme name here, which you want to open on mobile
    }
    else {
        return 'milano'; // set theme name here, which you want to open on other devices, like desktop
    }
}

add_filter( 'stylesheet', 'use_mobile_theme' );
add_filter( 'template', 'use_mobile_theme' );
Run Code Online (Sandbox Code Playgroud)