我无法使用remove_filter
覆盖父主题中的“阅读更多”链接。
以下是父主题函数:
/* Modify the \'[...]\' Read More Text */
add_filter( \'the_content_more_link\', \'hoot_modify_read_more_link\' );
if ( apply_filters( \'hoot_force_excerpt_readmore\', true ) ) {
add_filter( \'excerpt_more\', \'hoot_insert_excerpt_readmore_quicktag\', 11 );
add_filter( \'wp_trim_excerpt\', \'hoot_replace_excerpt_readmore_quicktag\', 11, 2 );
} else {
add_filter( \'excerpt_more\', \'hoot_modify_read_more_link\' );
}
/**
* Modify the \'[...]\' Read More Text
*
* @since 1.0
* @access public
* @return string
*/
function hoot_modify_read_more_link( $more = \'[…]\' ) {
if ( is_admin() )
return $more;
$read_more = esc_html( hoot_get_mod(\'read_more\') );
$read_more = ( empty( $read_more ) ) ? sprintf( __( \'Read More %s\', \'brigsby\' ), \'→\' ) : $read_more;
global $post;
$read_more = \'<a class="more-link" href="\' . esc_url( get_permalink( $post->ID ) ) . \'">\' . $read_more . \'</a>\';
return apply_filters( \'hoot_readmore\', $read_more ) ;
}
这是我在我的孩子主题函数中的函数。php:
function dw_remove_parent_read_more() {
remove_filter(\'excerpt_more\', \'hoot_modify_read_more_link\');
}
add_action(\'after_setup_theme\', \'dw_remove_parent_read_more\',999);
function dw_modify_read_more_link($more = \'[…]\') {
if ( is_admin() )
return $more;
$read_more = esc_html( hoot_get_mod(\'read_more\') );
$read_more = ( empty( $read_more ) ) ? sprintf( __( \'Read More %s\', \'brigsby\' ), \'\' ) : $read_more;
global $post;
$read_more = \'<div><a class="more-link" href="\' . esc_url( get_permalink( $post->ID ) ) . \'">\' . $read_more . \'</a></div>\';
return apply_filters( \'hoot_readmore\', $read_more ) ;
}
add_filter(\'excerpt_more\', \'dw_modify_read_more_link\', 999);
当前的结果是,现在显示了两个“阅读更多”链接,即父主题和我的子主题。我做错了什么:)?
SO网友:dwood7399
发现显示的“阅读更多”链接来自wp_trim_excerpt
挂钩而不是excerpt_more
钩只需要切换我的add_filter
和remove_filter
参考适当的挂钩。
function dw_remove_parent_read_more() {
remove_filter(\'wp_trim_excerpt\', \'hoot_replace_excerpt_readmore_quicktag\', 11, 2);
}
add_action(\'after_setup_theme\', \'dw_remove_parent_read_more\',11);
function dw_replace_excerpt_readmore_quicktag( $text, $raw_excerpt ) {
if ( is_admin() )
return $text;
$read_more = dw_modify_read_more_link();
$text = str_replace( \'<!--hoot-read-more-quicktag-->\', \'\', $text );
return $text . $read_more ;
}
add_filter(\'wp_trim_excerpt\', \'dw_replace_excerpt_readmore_quicktag\', 11,2);