【问题标题】:How to set the Theme programatically in WordPress?如何在 WordPress 中以编程方式设置主题?
【发布时间】:2015-06-12 05:45:54
【问题描述】:

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

例如这是我的网站网址: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.org/plugins/tags/theme-switcher
  • 您好 mevius,感谢您提供此 wordpress 功能的建议,我也尝试使用该功能,它工作正常,但此功能是永久激活主题我正在寻找它应该根据查询保持主题临时字符串网址。否则默认主题之前已激活...
  • @PriyankKhunt 也许您可以存储当前/默认主题名称,然后当用户使用不包含主题的 URL 访问时,重置为您存储在某处的那个。

标签: php wordpress wordpress-theming


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 2011-06-25
    相关资源
    最近更新 更多