1

I got this code:

function ntt_movie_shortcode($atts, $content = null)
{
    return 'TEST';
}
add_shortcode('ntt_movie', 'ntt_movie_shortcode');

The shortcode works perfectly on a single page, but on the Home Page / in excerpts the shortcodes are rendered as an empty string. So, the shortcode gets recognized but it seems, that the function doesn't get processed.

Any hints or approaches?

Thanks in advance.

0

If you’re using auto-generated excerpts, then all shortcodes will be removed (https://developer.wordpress.org/reference/functions/the_excerpt/):

An auto-generated excerpt will also have all shortcodes and tags removed. It is trimmed down to a word-boundary and the default length is 55 words. For languages in which words are (or can be) described with single characters (ie. East-Asian languages) the word-boundary is actually the character.

And it makes sense - you don’t want any advanced HTML in these excerpts and you have no control over what output will a shortcode generate (it may be something basic, but it may be a lot of complicated HTML code...)

|improve this answer|||||
0

In my case, I use (enclosing) shortcodes for formatting texts. So it's needed, that the shortcodes won't get removed because it removes words from sentences.

So I fiddled a bit and found this snippet, which helps to strip the shortcodes, but leaving the words in them: https://wordpress.stackexchange.com/a/229879/33303

It removes the default filter for the_excerpt and replaces it with a custom function. I presume, that this will lead me to additional problems ;) , but in this case it helps.

function ntt_custom_excerpt($text = '') {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');
                // $text = strip_shortcodes( $text );
        $text = do_shortcode( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt'  );
add_filter( 'get_the_excerpt', 'ntt_custom_excerpt'  );
|improve this answer|||||

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.