0

Lets say that I have this text in a field:

<p>The first expedition to the red planet has ended in disaster!</p>
<p>Your ship has crashed in the surface and all your equipment is lost. You managed to descent to safety with a single spacesuit and a limited amount of oxygen. Your only hope… to reach the rescue module before your air supply runs out!</p>
<p>With every step you are either closer to salvation or to your grave, be careful! Try finding the scattered remnants of your shipwreck across the&nbsp;dry landscape to help you in your journey; you will need the very precious o2 capsules and spare parts to upgrade your suit.</p>
<p>Hurry up, every second counts!</p>

If I do this, the text shows without problem:

$text = types_render_field('game-description', array());
echo $text;

But if I try to build automatically an excerpt, like this:

$text = types_render_field('game-description', array());
echo wp_trim_words($text, 65);

I realize that the <p>'s are all gone, leaving the text looking like this:

The first expedition to the red planet has ended in disaster! Your ship has crashed in the surface and all your equipment is lost. You managed to descent to safety with a single spacesuit and a limited amount of oxygen. Your only hope… to reach the rescue module before your air supply runs out! With every step you are either closer to salvation or to…

When I much would rather have it like this:

The first expedition to the red planet has ended in disaster!

Your ship has crashed in the surface and all your equipment is lost. You managed to descent to safety with a single spacesuit and a limited amount of oxygen. Your only hope… to reach the rescue module before your air supply runs out!

With every step you are either closer to salvation or to…

I know that you can write customs excerpts in each post, but the idea was to make it automatically and unload this task on the maintainer of the web.

Is there something I can do to fix this?

UPDATE: I forgot to say that I am using the extension Types, that is where the function types_render_field comes from. Basically to get the fields of the custom posts

2
  • types_render_field() is not a WordPress function. Where do you declare it?
    – fuxia
    Commented Apr 8, 2014 at 17:14
  • Sorry. I have updated the question Commented Apr 8, 2014 at 18:40

1 Answer 1

0

As toscho stated, types_render_field() is not a wordpress function, so you should have declared it somewhere.

HTML tags are stripped before returning the excerpt, whether you are using wp_trim_excerpt or wp_trim_words. If you need to keep HTML formatting in the excerpt, you will need to create your own excerpt function. To do this, you will first need to remove wp_trim_excerpt and then add your own function.

Here is a custom excerpt that wrote a while ago. It calles as normal in template files, meaning the_excerpt(). This excerpt keeps all HTML formatting. It also only breaks text after , ; ? . or ! The Read more text appear in a new paragraph, but if you need to have it next to the last word, you can just remove // from

//$pos = strrpos($pietergoosen_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$pietergoosen_excerpt = substr_replace($pietergoosen_excerpt, $excerpt_end, $pos, 0);
//else
// After the content

Here is my excerpt

if ( ! function_exists( 'pietergoosen_custom_wp_trim_excerpt' ) ) : 

    function pietergoosen_custom_wp_trim_excerpt($pietergoosen_excerpt) {
    global $post;
    $raw_excerpt = $pietergoosen_excerpt;
        if ( '' == $pietergoosen_excerpt ) {

            $pietergoosen_excerpt = get_the_content('');
            $pietergoosen_excerpt = strip_shortcodes( $pietergoosen_excerpt );
            $pietergoosen_excerpt = apply_filters('the_content', $pietergoosen_excerpt);
            $pietergoosen_excerpt = str_replace(']]>', ']]&gt;', $pietergoosen_excerpt);

            //Set the excerpt word count and only break after sentence is complete.
                $excerpt_word_count = 75;
                $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 
                $tokens = array();
                $excerptOutput = '';
                $count = 0;

                // Divide the string into tokens; HTML tags, or words, followed by any whitespace
                preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $pietergoosen_excerpt, $tokens);

                foreach ($tokens[0] as $token) { 

                    if ($count >= $excerpt_word_count && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) { 
                    // Limit reached, continue until , ; ? . or ! occur at the end
                        $excerptOutput .= trim($token);
                        break;
                    }

                    // Add words to complete sentence
                    $count++;

                    // Append what's left of the token
                    $excerptOutput .= $token;
                }

            $pietergoosen_excerpt = trim(force_balance_tags($excerptOutput));

                $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'pietergoosen' ), get_the_title()) . '</a>'; 
                $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

                //$pos = strrpos($pietergoosen_excerpt, '</');
                //if ($pos !== false)
                // Inside last HTML tag
                //$pietergoosen_excerpt = substr_replace($pietergoosen_excerpt, $excerpt_end, $pos, 0);
                //else
                // After the content
                $pietergoosen_excerpt .= $excerpt_end;

            return $pietergoosen_excerpt;   

        }
        return apply_filters('pietergoosen_custom_wp_trim_excerpt', $pietergoosen_excerpt, $raw_excerpt);
    }

endif; 

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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