2012-04-09 20 views
3

我似乎無法讓我的功能工作,以改變Twenty Eleven父主題的excerpt_more篩選器。爲什麼我不能通過我的孩子主題功能重寫WP的'excerpt_more'過濾器?

我懷疑它實際上可能是add_action('after_setup_theme', 'twentyeleven_setup');這就是問題所在,但我甚至試過remove_filter('excerpt_more', 'twentyeleven_auto_excerpt_more')擺脫二十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'); 

感謝功能,

Osu

+0

試試吧http://zeaks.org/enforce-and-customize-read-more-for-twenty-eleven-guest-post/ – 2012-04-09 23:49:18

+0

感謝您的建議,但我有同樣的問題。找到答案 - 您需要將remove_filters和add_filter包含到'after_setup_theme'調用中。現在將發佈解決方案。 – Osu 2012-04-12 08:09:09

回答

8

好吧,經過很多挫折後,我找到了一個解決方案(我認爲兒童主題是爲了加快速度!?)。我相信這是有效的,因爲一旦設置了父主題,'after_theme_setup'就會運行,這意味着您可以在那時刪除/覆蓋Twenty Eleven的功能。

如果我理解正確的話,根據該文件,兒童主題是第一次運行,那麼父母,然後「after_theme_setup」在你的子主題的functions.php文件的代碼位:

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 
+0

我以爲我一定要瘋了!謝謝! – Christina 2016-02-23 15:21:32

4

你自己的答案是複雜的事情,真的不應該是必要的。我無法解釋我的答案的原因,因爲我在另一個答案中找到了答案。但是無論如何,你總是可以在你的子主題中覆蓋父主題中的函數,儘管有時事實上你可以事先使用remove_filter(),或者,就像在這種情況下一樣,你所要做的就是增加你添加的過濾器的優先級,你的情況:

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

這應該是個訣竅。如果不是,請增加數量。 感謝this answer

相關問題