Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Trouble creating a manual excerpt

Hello fellow Coders!!

I want to create custom posts for my wp posts. Excerpts that include HTML tags like '''<p>,<strong>,<br>''' etc.

I followed the instructions to a post that was suggested by wordpress, copied and pasted the solution in the functions.php of my child theme and the excerpt has not changed. What could be the problem?

Here is the link to the article http://wordpress.stackexchange.com/questions/141125/allow-html-in-excerpt/141136#141136

Here is how my functions.php looks like.

'''php <?php require_once( trailingslashit( get_template_directory() ) . 'lib/init.php' );

function wpse_allowedtags() { // Add custom tags to this string return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>,<strong>'; }

if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :

function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
    if ( '' == $wpse_excerpt ) {

        $wpse_excerpt = get_the_content('');
        $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
        $wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
        $wpse_excerpt = str_replace(']]>', ']]&gt;', $wpse_excerpt);
        $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */

        //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', $wpse_excerpt, $tokens);

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

                if ($count >= $excerpt_length && 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;
            }

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

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

            //$pos = strrpos($wpse_excerpt, '</');
            //if ($pos !== false)
            // Inside last HTML tag
            //$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
            //else
            // After the content
            $wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */

        return $wpse_excerpt;   

    }
    return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}

endif;

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

?> '''

Please help!

Nthulane Makgato
Nthulane Makgato
Courses Plus Student 19,602 Points

I'm not sure but i think that Treehouse removed the last part of my question, with the code because its much longer. Please help.

4 Answers

you need to use the wordpress hooks if you wanna make any changes to the core of the wordpress. try this

function new_excerpt_more( $more ) {
    return ' ''',,''' '; // put in here whatever you want your excerpt to end in
}
add_filter('excerpt_more', 'new_excerpt_more');

Check the wordpress codex here for more info

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Thanks for responding Kevin. The appropriate hooks are used but they are not visible. The first function is for tags, not for the excerpt. In order to determine whether the hooks were used, please visit the link that i shared, under "Here is the complete code", is the code that i copied and pasted into my functions.php. Let me know what you think.

try past your code again or just put it somewhere put the link here so that we can help you mate

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

You are right Kelvin. Thank you very much for that thought.

Here is a link to a screenshot.

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Hey Kelvin, did the screen shot help you? Any idea what's wrong?