1

我有一个 wordpress 主题,但我将这个单一安装用于 2 个域。两个域都指向同一个安装。

现在,我想使用jQuery,根据地址栏中的域名,来改变WP网站的配色方案。

现在,在 WP-dashboard 中,您可以更改主题的颜色。现在,您可以选择主题颜色的界面在 rt_styling_options.php 文件中的以下代码中定义:

    array(
        "name"      => __("Theme Style",'rt_theme_admin'),
        "desc"      => __("Please choose a style for your theme.",'rt_theme_admin'),
        "id"        => THEMESLUG."_17_style",
        "options"   =>  array(
                        "blue"     => "Blue Style",
                        "purple"   => "Purple Style", 
                        "orange"   => "Orange Style",                       
                        "brown"   => "Brown Style",                                                 
                        "rose"   => "Rose Style",       
                        "green"   => "Green Style",     
                        "grey"   => "Grey Style",       
                        "gold"   => "Gold Style",                               
                        ),
        "default"   => "blue", 
        "type"      => "select"),  

这是 WP-Dashboard 的屏幕截图,您可以在其中选择此颜色:

在此处输入图像描述

问题:

如何根据地址栏中的域名更改该颜色的值?

它会是这样的:

var pathname = window.location.pathname;

if (pathname == 'domain2.com'){

    change the value of the theme color   

}

附加信息(重要):

这是调用上述颜色值的 php 文件 (theme.php) 和相关函数:( 参见 if(get_option(THEMESLUG."_17_style")){ )

function load_styles(){


        wp_register_style('theme-reset', THEMEURI . '/css/rt-css-framework.css', 1 , false, 'all');
        wp_register_style('theme-style-all',THEMEURI . '/css/style.css', 2 , false, 'all');

        if(get_option(THEMESLUG."_17_style")){          
            wp_register_style('theme-skin',THEMEURI . '/css/'.get_option( THEMESLUG."_17_style").'-style.css', 3 , false, 'all'); //dark skin               
        }

        wp_register_style('prettyPhoto',THEMEURI . '/css/prettyPhoto.css', 5 , false, 'screen');

        if(get_option(THEMESLUG."_font_face")){
            wp_register_style('rtfontface',THEMEURI . '/css/fontface.css', 100 , false, 'all');
        }       


        wp_enqueue_style('theme-reset');
        wp_enqueue_style('theme-style-all');  
        wp_enqueue_style('rtfontface'); 
        wp_enqueue_style('prettyPhoto');         
        wp_enqueue_style('jquery-colortip', THEMEURI . '/css/colortip-1.0-jquery.css');      
        wp_enqueue_style('jquery-jcarousel', THEMEURI . '/css/jcarousel.css');  
        wp_enqueue_style('jquery-flexslider', THEMEURI . '/css/flexslider.css');
        wp_enqueue_style('jquery-nivoslider', THEMEURI . '/css/nivo-slider.css');
        wp_enqueue_style('jquery-nivoslider-theme', THEMEURI . '/css/nivo-default/default.css'); 
        wp_enqueue_style('theme-skin');

        wp_register_style('theme-ie7',THEMEURI . '/css/ie7.css', 6 , false, 'screen');
        $GLOBALS['wp_styles']->add_data( 'theme-ie7', 'conditional', 'IE 7' );
        wp_enqueue_style('theme-ie7');

        wp_register_style('theme-ie8',THEMEURI . '/css/ie8.css', 6 , false, 'screen');
        $GLOBALS['wp_styles']->add_data( 'theme-ie8', 'conditional', 'IE 8' );
        wp_enqueue_style('theme-ie8');


        wp_register_style('theme-style',get_bloginfo( 'stylesheet_url' ), 4 , false, 'all'); //WP default stylesheet 
        wp_enqueue_style('theme-style');

        if (class_exists( 'Woocommerce' ) ) { //woocommerce style for rt-theme
            global $woocommerce, $suffix;

            wp_enqueue_style( 'rt-woocommerce-styles', THEMEURI.'/rt-woocommerce/woocommerce.css');

        }
    }



更新: @Patrick

theme.php的变化

   $themeId = "";
        $pathname = get_site_url();

        if($pathname == 'domain2.com')
       $themeId =  THEMESLUG."_17_style2";
        else 
           $themeId = THEMESLUG."_17_style";

        wp_register_style('theme-skin',THEMEURI . '/css/'.get_option($themeId).'-style.css', 3 , false, 'all'); 

注意:我从原来的 theme.php 文件中删除了这个:

if(get_option(THEMESLUG."_17_style")){  .....}

rt_styling_options.php中的更改:刚刚在第一组之后添加了以下内容,如您指定的那样

array(
        "name"      => __("Theme Style",'rt_theme_admin'),
        "desc"      => __("Please choose a style for your theme.",'rt_theme_admin'),
        "id"        => THEMESLUG."_17_style2",
        "options"   =>  array(
                        "blue"     => "Blue Style",
                        "purple"   => "Purple Style", 
                        "orange"   => "Orange Style",                       
                        "brown"   => "Brown Style",                                                 
                        "rose"   => "Rose Style",       
                        "green"   => "Green Style",     
                        "grey"   => "Grey Style",       
                        "gold"   => "Gold Style",                               
                        ),
        "default"   => "gold", 
        "type"      => "select"),  
4

2 回答 2

1

您绝对不能使用 JQuery 更改那些 Wordpress PHP 数组值。(JQuery 是客户端,而 PHP 是服务器端)。

您可以做的是使用 JQuery 根据 URL 路径名称来定位 CSS 和 HTML 元素……但这并不容易,因为您必须考虑受主题影响的所有元素。

或者,您必须使用 PHP 来执行基于 URL 路径切换主题之间的所有逻辑。

看起来您可以<?php get_site_url(); ?>在 Wordpress 中使用 PHP 获取 URL 路径。在此处查看更多信息:http: //codex.wordpress.org/Function_Reference/get_site_url

更新

您似乎可以通过直接调用 wordpress SQL 数据库来更改当前主题:

function switchTheme($theme) {
  global $wpdb;
  if (isset($theme)) {
    $queries = array("UPDATE wp_options SET option_value = 'default'
                      WHERE option_name = 'template';",
                     "UPDATE wp_options SET option_value = 'default'
                      WHERE option_name = 'stylesheet';",
                     "UPDATE wp_options SET option_value = 'default'
                      WHERE option_name = 'current_theme';");
    foreach ($queries as $query){
        $wpdb->query($query);
    }
  }
}

option_value是您放置主题名称的地方。

资料来源:

http://designgala.com/how-to-change-wordpress-theme-directly-from-database/ http://www.wprecipes.com/wordpress-trick-change-theme-programatically

还有来自 Wordpress的switch_theme和函数。update_option

与上述 SQL 查询类似,您可以使用update_option以下方式更新主题:

update_option('template', 'theme_name');
update_option('stylesheet', 'theme_name');
update_option('current_theme', 'theme_name');

http://codex.wordpress.org/Function_Reference/switch_theme http://codex.wordpress.org/Function_Reference/update_option

于 2013-07-24T20:15:28.643 回答
0

修改模板以具有多个颜色选项,给每个颜色一个唯一的 id

array(
    "name"      => __("Theme Style",'rt_theme_admin'),
    "desc"      => __("Please choose a style for your theme.",'rt_theme_admin'),
    "id"        => THEMESLUG."_17_style",
    "options"   =>  array(
                    "blue"     => "Blue Style",
                    "purple"   => "Purple Style", 
                    "orange"   => "Orange Style",                       
                    "brown"   => "Brown Style",                                                 
                    "rose"   => "Rose Style",       
                    "green"   => "Green Style",     
                    "grey"   => "Grey Style",       
                    "gold"   => "Gold Style",                               
                    ),
    "default"   => "blue", 
    "type"      => "select"),  
array(
    "name"      => __("Theme Style",'rt_theme_admin'),
    "desc"      => __("Please choose a style for your theme.",'rt_theme_admin'),
    "id"        => THEMESLUG."_17_style2",
    "options"   =>  array(
                    "blue"     => "Blue Style",
                    "purple"   => "Purple Style", 
                    "orange"   => "Orange Style",                       
                    "brown"   => "Brown Style",                                                 
                    "rose"   => "Rose Style",       
                    "green"   => "Green Style",     
                    "grey"   => "Grey Style",       
                    "gold"   => "Gold Style",                               
                    ),
    "default"   => "blue", 
    "type"      => "select"),  

然后在 theme.php

$themeId = "";
    if($pathname == 'domain2.com')
       $themeId =  THEMESLUG."_17_style2";
    else 
       $themeId = THEMESLUG."_17_style";

    wp_register_style('theme-skin',THEMEURI . '/css/'.get_option($themeId).'-style.css', 3 , false, 'all');
于 2013-07-24T20:19:05.653 回答