最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Create different flavours of excerpt

programmeradmin3浏览0评论

I have an understanding problem with the Wordpress filter approach.

I thought that I could easily use something like the_excerpt() on one hand and create the the_excerpt_function_for_this_special_page() on the other hand. However, it turns that I'm really lost between the filters and functions and I do not know how to achieve my goal.

What I want to build

I added a filter to the get_the_excerpt filter as follows:

function the_readmore_excerpt( $excerpt ) { 
    return $excerpt. '.. <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('Mehr lesen', 'baskerville') . ' &rarr;</a>'; 
} 

add_filter('get_the_excerpt', 'the_readmore_excerpt');

That's working well except that it appears on every excerpt on the website (that's actually expected).

Question

How do I get 2 different excerpts: one with read more and one without (based on this code)?

I have an understanding problem with the Wordpress filter approach.

I thought that I could easily use something like the_excerpt() on one hand and create the the_excerpt_function_for_this_special_page() on the other hand. However, it turns that I'm really lost between the filters and functions and I do not know how to achieve my goal.

What I want to build

I added a filter to the get_the_excerpt filter as follows:

function the_readmore_excerpt( $excerpt ) { 
    return $excerpt. '.. <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('Mehr lesen', 'baskerville') . ' &rarr;</a>'; 
} 

add_filter('get_the_excerpt', 'the_readmore_excerpt');

That's working well except that it appears on every excerpt on the website (that's actually expected).

Question

How do I get 2 different excerpts: one with read more and one without (based on this code)?

Share Improve this question asked Mar 14, 2020 at 19:38 Laurent MeyerLaurent Meyer 1011 bronze badge 2
  • 1 on what condition should that choice be based? i.e. what would distinguish the two different excerpts? – Michael Commented Mar 14, 2020 at 20:22
  • @Michael I would call the read more in the post-in-page-loop from here github/ivycat/posts-in-page/blob/master/templates/… and the other would stay used by the system. – Laurent Meyer Commented Mar 15, 2020 at 11:40
Add a comment  | 

1 Answer 1

Reset to default 0

Probably it's not the best approach because it requires introducing a global variable but here's the idea:

In Posts_in_Page plugin there are 2 filters, one is fired before output of these items posts_in_page_pre_loop, and one after output end posts_in_page_post_loop. So the plan is to create global variable which will be set to true when you are in this plugin's loop and to false when it's outside, so in your excerpt filter you'll be able to check it.

So here's how it can be done:

/*
 * 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') . ' &rarr;</a>';
    }

    return $excerpt; // return default excerpt if we are not in this custom plugin's loop.
}

add_filter('get_the_excerpt', 'the_readmore_excerpt');

Again it's probably not a perfect solution because of introducing a global variable, but it will work.

Plugin's filters hooks are got from here https://github/ivycat/posts-in-page/blob/master/includes/class-page-posts.php (lines 364, 373)

发布评论

评论列表(0)

  1. 暂无评论