4

excerpt_moreTwentyEleven親テーマのフィルターを変更するために関数を機能させることができないようです。

それが実際にadd_action( 'after_setup_theme', 'twentyeleven_setup' );問題である可能性があると思いますが、私はremove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' )Twenty Elevenの関数を削除しようとしましたが、それでも私の関数は何も変更していません...

手伝ってくれますか?

これが完全なfunctions.phpコードです:

http://pastie.org/3758708

これが私が/mychildtheme/functions.phpに追加した関数です

function clientname_continue_reading_link() {
    return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Read more... <span class="meta-nav">&rarr;</span>', 'clientname' ) . '</a>';
}
function clientname_auto_excerpt_more( $more ) {
    return ' &hellip;' . clientname_continue_reading_link();
}
add_filter( 'excerpt_more', 'clientname_auto_excerpt_more' );

ありがとう、

大須

4

2 に答える 2

9

さて、多くの欲求不満の後で、私はこれに対する解決策を見つけました(私は子供テーマが物事をスピードアップすることを意図していると思いました!?)。親テーマが設定されると「after_theme_setup」が実行されるため、これは機能すると思います。つまり、その時点でTwentyElevenの関数を削除/オーバーライドできます。

私が正しく理解していれば、このドキュメントによると、最初に子テーマが実行され、次に親が実行され、次に子テーマのfunctions.phpファイルの「after_theme_setup」コードが実行されます。

http://codex.wordpress.org/Child_Themes#Using_functions.php

http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme

これは私の子テーマのfunctions.phpファイルにあるものです。これが誰かに役立つことを願っています:

// ------------------------------------------------------------------
//                      // !AFTER_SETUP_THEME
// ------------------------------------------------------------------

/* Set up actions */
add_action( 'after_setup_theme', 'osu_setup' );

if ( ! function_exists( 'osu_setup' ) ):

function osu_setup() {

    // OVERRIDE : SIDEBAR GENERATION FUNCTION - NO WIDGETS FOR THIS SITE
    remove_action( 'widgets_init', 'twentyeleven_widgets_init' ); /* Deregister sidebar in parent */

    // OVERRIDE : EXCERPT READ MORE LINK FUNCTION
    function osu_readon_link() {
        return '...<a href="'. get_permalink() . '" class="readmore">' . __( 'Read More...', 'clientname' ) . '</a>';
    }
    // Function to override
    function osu_clientname_custom_excerpt_more( $output ) {
        if ( has_excerpt() && ! is_attachment() ) {
            // $output = trim($output);
            $output .= osu_readon_link();
        }
        return $output;
    }
    remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
    add_filter( 'get_the_excerpt', 'osu_clientname_custom_excerpt_more' );
    remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
    add_filter( 'excerpt_more', 'osu_readon_link' );

    // OVERRIDE : EXCERPT LENGTH FUNCTION
    function osu_clientname_excerpt_length( $length ) {
        return 30;
    }
    remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
    add_filter( 'excerpt_length', 'osu_clientname_excerpt_length' );

}
endif; // osu_setup
于 2012-04-12T08:12:01.630 に答える
5

あなた自身の答えは物事を複雑にしているので、本当に必要ではないはずです。別の答えで見つけたので、答えの理由を説明できませんでした。ただし、どのような場合でも、子テーマの親テーマの関数を常にオーバーライドできますが、実際にはremove_filter()事前に使用することもあります。または、この場合のように、追加したフィルターの優先度を上げるだけです。場合:

add_filter( 'excerpt_more', 'clientname_auto_excerpt_more', 11 );

それでうまくいくはずです。そうでない場合は、数を増やします。この答えに感謝します:

于 2016-11-24T11:57:27.537 に答える