2013-01-20 29 views
0

我開發了一個使用WordPress的博客網站。在我準備好主題後,網站所有者要求我使用他爲該網站的博客購買的其他主題。在Wordpress中初始化第二個主題只爲博客

如何初始化第二個主題而不將其設置爲選項表上的網站主題?

我能做到這一點定義常量TEMPLATEPATHwp-includes/default_constants.php

function 'wp_templating_constants': 
define('TEMPLATEPATH', get_theme_root() . '/theme-name'); 

和設置過濾器:

pre_option_templatetemplatepre_option_current_themestylesheet_directory_uristylesheet_directory在主題的functions.php。

但是當然,我想在知道用戶是否在博客頁面之後動態地執行此操作。有沒有人有任何想法如何做到這一點?

回答

1

下面是插件的代碼,用於將主題設置爲不是數據庫中定義的主題的類別列表。

new SetTheme('[THEME_NAME]', array('[CAT_NAME]', [CAT_ID], [CAT_OBJECT])); 

class SetTheme { 
    private $theme_name = ''; 
    private $categories = array(); 

    public function __construct($theme_name, $categories) { 
     // define original theme location for any reason 
     define('ORIGTEMPLATEPATH', get_template_directory()); 
     define('ORIGTEMPLATEURI', get_template_directory_uri()); 

     // init class parameters 
     $this->theme_name = $theme_name; 

     foreach ($categories as $cat) { 
      if (is_string($cat)) 
       $cat = get_category_by_slug($cat); 

      $category = get_category($cat); 
      $this->categories[$category->term_id] = $category; 
     } 

     // apply action to setup the new theme only on action 'setup_theme' 
     // because some functions are not yet loaded before this action 
     add_action('setup_theme', array($this, 'setup_theme')); 
    } 

    public function setup_theme() { 
     // if the current post or category is listed, apply the new theme to be initialized 
     if ($this->is_category_theme()) 
      $this->set_theme(); 
    } 

    private function is_category_theme() { 
     // get category from current permalink 
     // and check if is listed to apply the new theme 
     $current_cat = get_category_by_path($_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'], false); 
     if (isset($this->categories[$current_cat->term_id])) return true; 

     // get post from current permalink 
     // and check if it belongs to any of listed categories 
     $current_post = url_to_postid($_SERVER['REQUEST_URI']); 
     $post_categories = wp_get_post_categories($current_post); 
     foreach ($post_categories as $cat_id) 
      if (isset($this->categories[$cat_id])) return true; 

     return false; 
    } 

    private function set_theme() { 
     // apply the filters to return the new theme's name 
     add_filter('template', array($this, 'template_name')); 
     add_filter('stylesheet', array($this, 'template_name')); 
    } 

    public function template_name() { 
     // return new name 
     return $this->theme_name; 
    } 
} 

該類的參數是主題名稱和類別(IDS,子彈或類別對象)的數組。

當然,這是我所需要的,可能對於其他主題,它將需要在函數'set_theme'中的其他過濾器。

它需要是一個插件,因爲插件在主題之前加載,甚至在WP類之前加載。

有了這個插件,原來的插件永遠不會被調用(至少在我的情況下)。

相關問題