使用具有不同 wordpress 主题的两个域相同的数据库 [英] using two domain same database with different wordpress theme

查看:17
本文介绍了使用具有不同 wordpress 主题的两个域相同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用相同的数据库(内容/用户/评论/元/类别等)在我的子目录中安装另一个 wordpress.

I want to use, same database, (content / users / comments / meta's / categories etc.) for another wordpress install in my sub directory.

我实际上想为我的网站创建移动版本.但我不想使用任何移动检测脚本或 css3 媒体查询.只是想创建我的新主题(移动版)

I actually want to create mobile version of my site. But i dont want to use, any mobile detect script ory css3 media queries. Just want to create my new theme (for mobile version)

例如;

主域也有子域,如;

maindomain.com // root
mobile.maindomain.com // sub directory

这怎么可能?

推荐答案

WordPress 的一大优点是代码中的大量钩子允许您扩展或覆盖核心功能.

One of the nice things about WordPress is the great number of hooks in the code allowing you to extend or override core functionality.

解决此问题的一种方法是在您的 vhost 文件中为每个站点设置一个 Apache 环境变量,该变量可在 WordPress 引导过程中使用以覆盖主题和基本 URL 设置.

One way to approach this problem would be to set an Apache environment variable in your vhost file for each site that could be used in the WordPress bootstrap process to over-ride the theme and base URL setup.

例如在 Apache 虚拟主机中添加:

e.g. in Apache vhost add:

SetEnv WP_CONTEXT main

SetEnv WP_CONTEXT mobile

(或等效的,如果您使用不同的网络服务器).

(or equivalent if you're using a different webserver).

在 wp-config.php 中:

In wp-config.php:

switch ($_SERVER['WP_CONTEXT']) {
    case 'main':
        define('WP_HOME','http://maindomain.com');
        define('WP_SITEURL','http://maindomain.com');
    break;

    case 'mobile':
        define('WP_HOME','http://mobile.maindomain.com');
       define('WP_SITEURL','http://mobile.maindomain.com');
    break;
}

这将根据环境变量设置基本 URL.

This will set the base URLs based on the environment variable.

然后在插件中添加以下过滤器:

Then in plugin add the following filters:

add_filter('template', 'change_theme');
add_filter('option_template', 'change_theme');
add_filter('option_stylesheet', 'change_theme');

function change_theme() 
{
    switch ($_SERVER['WP_CONTEXT']) {
        case 'main':
            return 'main';
        break;

        case 'mobile':
           return 'mobile';
        break;
}

这需要在插件中,以便在正常主题加载过程之前加载(functions.php 是主题的一部分,因此为时已晚).这些过滤器将拦截并覆盖数据库中的主题设置.

This needs to be in a plugin so that it's loaded before the normal theme loading process (functions.php is part of the theme and hence too late). These filters will intercept and over-ride the theme settings from the database.

这篇关于使用具有不同 wordpress 主题的两个域相同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆