サポート » テーマ » TwentyTenの子テーマでウィジェットを上書きしたい

  • 解決済 tomotomo

    (@tomotomo)


    TwentyTenの子テーマでウィジェット名を変更したいので、子テーマのfunctions.phpに下記のように書いたのですが、管理画面のウィジェット画面に反映されません。

    function my_widgets_init() {
        register_sidebar( array(
            'name' => 'ブログのウィジェットエリア',
            'id' => 'primary-widget-area',
            'description' => 'ブログのウィジェットエリア',
            'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
            'after_widget' => '</li>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        ) );
    
        register_sidebar( array(
            'name' => 'ページのウィジェットエリア',
            'id' => 'secondary-widget-area',
            'description' => 'ページのウィジェットエリア',
            'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
            'after_widget' => '</li>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        ) );
    }
    
    add_action( 'widgets_init', 'my_widgets_init' );

    子テーマでウィジェットを上書きするにはどのように書いたらよいのでしょうか?

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • TwentyTen の functions.php ソースのウィジェット登録の始めに

    To override twentyten_widgets_init() in a child theme, remove the action hook and add your own function tied to the init hook.

    てなコメントがあるので、子テーマの functions.php で

    remove_action( 'widgets_init', 'twentyten_widgets_init' );
    add_action( 'widgets_init', 'my_widgets_init' );

    とすれば良いような気がします(未検証)。

    トピック投稿者 tomotomo

    (@tomotomo)

    kzさん こんにちは

    ご提示頂いたとおりにadd_action()の前に

    remove_action( 'widgets_init', 'twentyten_widgets_init' );

    を追加したのですが、ウィジェット画面に反映されませんでした >_<

    http://wpdocs.sourceforge.jp/Child_Themes
    Codexの解説によれば子テーマのfunctions.phpのあとに親テーマのfunctions.phpが読込まれるようなので、子テーマでremove_actionしても意味がないように思えます。
    Twenty Tenのコメントも違っているような気がします。
    現在は親テーマfunctions.phpのactionやfilterを子テーマで変更する方法が提供されてないような気がします。

    今回は、親テーマのadd_action( 'widgets_init', 'twentyten_widgets_init' );をコメントにするしか他に手段が無いように思えるのですが。。。

    どなたか、方法をご存知の方がいらっしゃれば教えていただきたいと思います。
    と書いてしまいましたが、対応策があるようです。

    add_action( 'after_setup_theme', 'child_theme_setup' );
    if ( !function_exists( 'child_theme_setup' ) ):
    function child_theme_setup () {
    	remove_action( 'widgets_init', 'twentyten_widgets_init' );
    }
    endif;

    で、どうでしょうか?
    本家Forum
    http://wordpress.org/support/topic/can-i-create-child-functionsphp-file-for-twentyten?replies=26

    トピック投稿者 tomotomo

    (@tomotomo)

    taikikenさん こんにちは

    Codexの解説によれば子テーマのfunctions.phpのあとに親テーマのfunctions.phpが読込まれるようなので、子テーマでremove_actionしても意味がないように思えます。
    Twenty Tenのコメントも違っているような気がします。

    そうだったんですね。
    Twenty Tenのコメントが間違っているとは思いもよりませんでした。

    でも、教えていただいたコードで上手く反映されました。
    ありがとうございました!!

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「TwentyTenの子テーマでウィジェットを上書きしたい」には新たに返信することはできません。