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

标签 php wordpress wordpress-theming

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

例如这是我的站点 URL:http://example.com

我想通过传递一个查询参数来切换主题:

http://example.com?mytheme=red_theme
http://example.com?mytheme=blue_theme
etc.

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

http://example.com/blog?mytheme=red_theme

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

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

http://example.com/blog

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

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

最佳答案

在 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' );

关于php - 如何在 WordPress 中以编程方式设置主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796038/

相关文章:

php - 如何确保在生成 PDF 之前加载图像?

javascript - 如何利用浏览器宽度和body宽度动态计算固定div元素的定位宽度

mysql - Woocommerce MySQL 更新特定类别产品的价格

PHP: $myObject = new ClassName(); vs newClassName;

php - WordPress CSRF 利用草稿状态

php - 多个 PHP session

WordPress htaccess 永久链接

php - Wordpress 主题错误 "Fatal error: Cannot re-assign auto-global variable _POST"

css - 带侧页眉的粘性页脚

php - 在类别页面 WooCommerce 上内联显示添加到购物车按钮