个性化阅读
专注于IT技术分析

如何在上线之前自定义WordPress主题

昨天, 我在自托管网站上的Wordpress上安装了新主题。我知道该功能使你可以预览主题并使用它来选择我要安装的新主题。

问题我不想中断我的网站的正常运行, 但是这个新主题需要大量的自定义才能准备就绪。我该怎么做呢?

我的解决方案是在桌面上运行虚拟服务器的唯一方法吗?这似乎很乏味, 更不用说在执行此操作时切换到”真实”服务器时通常会遇到的所有错误。

有更好的方法吗?我一直在SO以及WordPress论坛上搜索有关如何执行此操作的答案, 但结果很简短。我本以为这是一个普遍的问题。也许我使用了错误的搜索词[主题, 自定义, 安装前]?

任何帮助是极大的赞赏!谢谢!


#1


好的, 因为你的问题是一个很好的问题, 并且可能很少有人在决定更新其站点时正在经历相同的过程, 所以我决定尝试一下get_stylesheet和get_template过滤器挂钩。事实证明, 使用非常小的插件, 你可以根据特定规则轻松实施特定主题(在这种情况下, 任何登录的访问者都可以使用, 但是你可以更改此主题以使用所需的任何逻辑)。

这是你需要在插件目录中的文件中放入的代码:

<?php 
/*
Plugin Name: Switch Theme
Description: Switches the theme for logged-in visitors, while keeping the current theme for everyone else. !!!NOTE!!! Please back-up your database prior using this plugin - I can't guarantee that it will work with any theme, nor that it won't break your site's set-up - USE AT YOUR OWN RISK(I did a quick test and it seemed to be fine, but haven't done extensive testing).

You don't need to switch to the desired theme before that - you want to keep active the theme that you will display to your visitors - the one that you will see will be used programatically.

Before activating the plugin, change the line that says `private $admin_theme = '';` to `private $admin_theme = 'theme-directory-name';` where "theme-directory-name" is obviously the name of the directory in which the desired theme resides in.
*/

class MyThemeSwitcher {
    private $admin_theme = '';

    function MyThemeSwitcher() {
        add_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) );
        add_filter( 'template', array( &$this, 'get_template' ) );
    }

    function get_stylesheet($stylesheet = '') {
        if ( is_user_logged_in() && $this->admin_theme ) {
            return $this->admin_theme;
        }
        return $stylesheet;
    }

    function get_template( $template ) {
        if ( is_user_logged_in() && $this->admin_theme ) {
            return $this->admin_theme;
        }
        return $template;
    }
}

$theme_switcher = new MyThemeSwitcher();

所以-首先备份你的数据库!我在本地测试了”二十一”作为默认主题, 并将基本框架主题作为自定义主题-主题选项和导航菜单已正确保存。

然后, 你需要做的就是更新文件(将显示为private $ admin_theme =的行更改为private $ admin_theme =’theme-slug’;其中theme-slug是你在其中放置主题的目录的名称想要使用的是)。

另外-你将无法更改”首页”和”帖子”页面选项, 而这不会影响实时站点, 也无法更改两个主题都使用的任何共享组件(网站名称, “首页”, “帖子”页面, 每页帖子数, 等等选项, 内容等)。

因此, 如果你不知道此解决方案是否适合你-那就取决于。

如果这两个主题都不是相对复杂的, 那么最有可能你应该能够使用此技巧。如果是这样, 也许你应该按照其他人的建议进行网站的第二次安装-我认为在子域或子目录中进行第二次安装将是你的最佳选择(这是因为移动多站点数据库可以比移动普通的WP数据库要复杂得多)。


#2


我会在安装了wordpress的本地apache服务器上进行设置, 以自定义和测试新主题。完成自定义之后, 你可以将主题上载到实时站点并激活它。如果需要在仪表板中进行设置, 则可能必须再次进行调整。这是在发布主题之前测试/自定义主题的一种方法。


#3


你可以创建一个网络(使用define(‘WP_ALLOW_MULTISITE’, true)创建WordPress多站点;请参阅:http://codex.wordpress.org/Create_A_Network), 然后创建一个子站点, 然后使用维护插件, 因此非管理员身份登录的用户无法使用它, 无法从主博客中导出你的帖子和数据, 使用WordPress默认导入程序将其导入子博客中, 然后将新主题应用于此子博客并进行处理。当一切都让你满意时, 将主题应用于主站点并停用子站点。

赞(0)
未经允许不得转载:srcmini » 如何在上线之前自定义WordPress主题

评论 抢沙发

评论前必须登录!