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)?

2
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.com/ivycat/posts-in-page/blob/master/includes/class-page-posts.php (lines 364, 373)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.