Excerpt and Yoast Error


Hi, I'm using the following code to get the excerpt from within functions but it's throwing up an error when I have Yoast installed and I can't work out why.

The code I'm using is:

// Gets the excerpt
add_filter( 'the_content', 'get_post_excerpt' );

function get_post_excerpt( $content ) {

    if ( has_excerpt() ) {
        return '<p>' . get_the_excerpt() . '</p><p>' . beans_post_more_link() . '</p>';
    }

    return $content;
}

It works fine on the front end but when I go to publish or update a post with Yoast installed it just goes to a white screen and without Yoast there's no error. With debug enabled it's telling me it's down to the return line. I've narrowed this down to the "beans_post_more_link()" because if I remove it to read:

return '<p>' . get_the_excerpt() . '</p>';

everything works fine.

I'm not great at php and new to beans so I was wondering if it's something obvious i'm missing.

Thanks


I've tried a few variations of getting the excerpt now and it throws up the same error every time when I have Yoast activated and always points to the line with beans_post_more_link() on, if I remove it everything works fine. Could this be a bug?

Here's the main parts of the error, I've just removed the parts with my server directory URLs.

Fatal error: Uncaught Error: Call to undefined function beans_post_more_link() /directory/functions.php:671 This is always the line with the "beans_post_more_link()" on.

Stack trace: #0 /wp-includes/class-wp-hook.php(286): beans_child_modify_post_content

This seems to refer to the line beans_add_smart_action( 'the_content_more_link', 'beans_post_more_link' ); within beans post.php in fragments.


Hi Keith,

In your function, If the post has excerpt, you are returning two values at the same time, and that is not possible. In this case you are returning only the string, without the content.

You should concatenate $content + $your_string and return them together in one only variable.

Hope it helps. Regards,


Hello, I was having the same problem on a staging site I was working on, and found the solution.

You need to move the beans_post_more_link to after the $content, like so:

return '<p>' . get_the_excerpt() . '</p>';
    }

    return $content . '<p>' . beans_post_more_link()  . '</p>';
}

Hope that helps. It did for me!


UPDATE: Ok, I just noticed if I do what I posted above, I get the Continue Reading... on my single posts too. After more experimenting, this is what I came up with:

// Show excerpt except on Single Post.
add_filter( 'the_content', 'example_modify_post_content' );

function example_modify_post_content( $content ) {

 // Return the excerpt() if it exists and if it is not the single post.
    if(is_singular())
        return $content;
    else if ( has_excerpt() && ( !is_singular() ) ) 
        return '<p>' . get_the_excerpt() . '</p>'; 
}
// Add the Continue Reading... to non-single posts
add_filter('beans_post_content_after_markup', 'example_add_some_more');

function example_add_some_more() {
    if ( !is_singular() ) { ?> 
        <p>
            <?php echo beans_post_more_link(); ?>
        </p>
<?php } 
}

Write a reply

Login or register to write a reply, it's free!