web-dev-qa-db-ja.com

カスタム分類を追加した後にcustomize_registerアクションを追加する方法

customize_registerはcustomize_registerフックでのみ機能します。カスタム分類法を追加するために使用するフックに関係なく、それはcustomize_registerの後に必ず発生します。

(カスタム分類法の用語リストをループして、各用語のカテゴリとカスタムオプションを作成します)

編集:

これは私のテンプレート関数ファイルの問題になりがちなコードです。

register_taxonomy( 'brand', $object_types, $args );
add_action( 'muplugins_loaded', 'brand', 0 );
// ????? What Hook can I use here^ that will run before the 'customize_register' hook ????? //

function iartz_customize_register( $wp_customize ) {
    $brands = get_terms('brand');
    foreach($brands AS $brand){
        //Add color options for each term of taxtonomy 'brand'
    }
}
add_action( 'customize_register', 'iartz_customize_register' );
2

customizer_registerフック状態で分類法データを取得することは問題にならないはずです。だからその問題は異なる理由があるかもしれませんが、それらはあなたの質問から明らかではありません。あなたが何を達成しようとしているのか私にはわからないので、あなたは@ Rarstの質問に従って情報のギャップを埋めたいと思うかもしれません。それ以外に、そこに複数の方法があります - 例えば - 分類法ドロップダウンをカスタマイザに追加する - 私は1つを選び、別のものをリンクしました、下記参照。


免責事項:
これは私のコードではありません、完全を期すためにここに追加します。ソースへのリンクを追加します。自分で試したことはありませんが、私が言うことができる限りそれには何も悪いことはなく、それは人々のために働いています。


テーマカスタマイザの分類ドロップダウンのコード

要旨 の要旨
→Eric Judenによるブログ の記事 に従って

これはfunctions.phpに入ります

add_action('customize_register', 'my_customize_register');
function my_customize_register($wp_customize){
require_once(TEMPLATEPATH . '/class/wp_customizer_taxonomy_dropdown.php');

$wp_customize->add_section('my_theme_blog_featured_categories', array(
    'title' => __('Blog: Featured Categories'),
    'priority' => 36,
));

$wp_customize->add_setting('featured_category_1', array(
    'default' => get_option('default_category', ''),
));

$wp_customize->add_control( new Taxonomy_Dropdown_Customize_Control($wp_customize, 'featured_category_1', array(
    'label' => __('Featured Area 1'),
    'section' => 'my_theme_blog_featured_categories',
    'settings' => 'featured_category_1',
    'args' => array(), // arguments for wp_dropdown_categories function...optional. array('taxonomy' => 'my_taxonomy')
)));

return $wp_customize;
}

これは、classと呼ばれる、functions.phpに相対して位置するフォルダーに置かれたwp_customizer_taxonomy_dropdown.phpと呼ばれるファイルに入ります:

class Taxonomy_Dropdown_Customize_Control extends WP_Customize_Control {
    public $type = 'taxonomy_dropdown';
    var $defaults = array();
    public $args = array();

    public function render_content(){
        // Call wp_dropdown_cats to ad data-customize-setting-link to select tag
        add_action('wp_dropdown_cats', array($this, 'wp_dropdown_cats'));

        // Set some defaults for our control
        $this->defaults = array(
            'show_option_none' => __('None'),
            'orderby' => 'name', 
            'hide_empty' => 0,
            'id' => $this->id,
            'selected' => $this->value(),
        );

        // Parse defaults against what the user submitted
        $r = wp_parse_args($this->args, $this->defaults);

?>
    <label><span class="customize-control-title"><?php echo esc_html($this->label); ?></span></label>
<?php  
        // Generate our select box
        wp_dropdown_categories($r);
    }

    function wp_dropdown_cats($output){
        // Search for <select and replace it with <select data-customize=setting-link="my_control_id"
        $output = str_replace('<select', '<select ' . $this->get_link(), $output);
        return $output;
    }
}

リンクされた追加情報とこれはほとんど自己説明的であるべきです。

追加の同様のアプローチ

WordPressテーマカスタマイザカスタムコントロール by @bueltge;
→要旨の分類ドロップダウン例の ソース へのリンク。
→このトピックに関する彼の 答え

→利用可能なチュートリアル、記事などがいくつかあります。必要に応じて自分で簡単に見つけることができます。

2
Nicolai

短い答え:両方の関数はcustomize_registerにフックすることができ、それらはうまく機能します。 customize_registerフックはcustomizeページでのみ機能しますが、両方のページでregister_taxonomy関数が必要です。そのため、フロントエンドのページ呼び出しでregister_taxonomy関数を使用するために、initフックでも呼び出しました。


「その問題には異なる理由があるかもしれません」

ialocin、その通りです….

それほど深くならずに、私はcustomize_registerフックがすべてのページ呼び出しで呼び出されることを期待していましたが(それはドキュメンテーションが言っていることであるので)、それはしません。

助けてくれてありがとう!

1