可能这不是最好的方法,因为它需要引入一个全局变量,但想法如下:
在里面Posts_in_Page
插件有2个过滤器,其中一个在输出这些项目之前启动posts_in_page_pre_loop
, 输出端后一个posts_in_page_post_loop
. 因此,计划是创建全局变量,该变量将设置为true
当你在这个插件的循环中false
当它在外面时,所以在摘录过滤器中,您可以检查它。
下面是如何做到这一点:
/*
* Sets our global flag that we are inside this plugin\'s specific loop. And returns content untouched.
*/
function my_custom_set_in_posts_pages_loop($output){
global $custom_loop_is_in_posts_in_page_post_loop;
$custom_loop_is_in_posts_in_page_post_loop = true;
return $output; // output is untouched we return it as is, because we only switch our flag.
}
add_filter(\'posts_in_page_pre_loop\', \'my_custom_set_in_posts_pages_loop\');
/*
* Sets our global flag that we are inside this plugin\'s specific loop. And returns content untouched.
*/
function my_custom_set_out_of_posts_pages_loop($output){
global $custom_loop_is_in_posts_in_page_post_loop;
$custom_loop_is_in_posts_in_page_post_loop = false;
return $output; // output is untouched we return it as is, because we only switch our flag.
}
add_filter(\'posts_in_page_post_loop\', \'my_custom_set_out_of_posts_pages_loop\');
function the_readmore_excerpt( $excerpt ) {
global $custom_loop_is_in_posts_in_page_post_loop;
if( isset($custom_loop_is_in_posts_in_page_post_loop) && $custom_loop_is_in_posts_in_page_post_loop ){
return $excerpt. \'.. <a class="read-more" href="\'. get_permalink( get_the_ID() ) . \'">\' . __(\'Mehr lesen\', \'baskerville\') . \' →</a>\';
}
return $excerpt; // return default excerpt if we are not in this custom plugin\'s loop.
}
add_filter(\'get_the_excerpt\', \'the_readmore_excerpt\');
同样,由于引入了全局变量,这可能不是一个完美的解决方案,但它会起作用。
插件的过滤器挂钩来自这里https://github.com/ivycat/posts-in-page/blob/master/includes/class-page-posts.php (第364373行)