0

ワードプレスを使ってブログ付きのサイトを作りました。テーマを準備した後、ウェブサイトの所有者から、ウェブサイトのブログ専用に購入した別のテーマを使用するように言われました。

オプション テーブルで Web サイトのテーマとして設定せずに、この 2 番目のテーマを初期化するにはどうすればよいですか?

TEMPLATEPATHで定数を定義することができましたwp-includes/default_constants.php

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

そして、次のフィルターを設定します。

pre_option_template、、、、、テーマのfunctions.phptemplate内。pre_option_current_themestylesheet_directory_uristylesheet_directory

しかしもちろん、ユーザーがブログページにいるかどうかを確認した後、動的に実行したいと考えています。誰もそれを行う方法を知っていますか?

4

1 に答える 1

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;
    }
}

クラスのパラメータは、テーマ名とカテゴリの配列 (ID、スラッグ、またはカテゴリ オブジェクト) です。

もちろん、これは私が必要とするものです。おそらく他のテーマでは、関数「set_theme」の他のフィルターが必要になります。

プラグインはテーマの前、さらには WP クラスの前にロードされるため、プラグインである必要があります。

このプラグインを使用すると、元のプラグインが呼び出されることはありません (少なくとも私の場合)。

于 2013-01-21T15:12:28.287 に答える