WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to show links in excerpt? (5 posts)

  1. secretja
    Member
    Posted 3 years ago #

    Hi,

    is there any way to show links from post in excerpt? I lost 2h on google but didn't find answer.

    Thank you.

  2. Curtiss Grymala
    Member
    Posted 3 years ago #

    If you use the custom post excerpt box, you can use HTML in there. Otherwise, you'd have to create new filters for the_excerpt and stop it from stripping the tags; but that could result in some catastrophes on your site (for instance, if the link starts within the area that would be part of the excerpt, but ends outside of the excerpt area, you'd end up with an open link tag that never gets closed on your archive/home pages).

  3. secretja
    Member
    Posted 3 years ago #

    I understand this potential css problems but I can't find how to make filter to stop striping html from excerpts. Any tip?

    Thank you on reply.

  4. Curtiss Grymala
    Member
    Posted 3 years ago #

    You will first need to remove the wp_trim_excerpt filter from get_the_excerpt.

    remove_filter( 'the_excerpt', 'wp_trim_excerpt' );

    Then, you'll need to write your own function that will perform most of the same actions that wp_trim_excerpt performs. You can find the wp_trim_excerpt() function in wp-includes/formatting.php for reference. You might even just want to copy that function into your theme's functions.php file, give it a different name (so you don't have a function name clash) and then change the strip_tags($text) call inside that function to strip_tags($text,'<a>').

  5. secretja
    Member
    Posted 3 years ago #

    Thank you on tips.
    Here is how I do that.

    function new_trim_excerpt($text) {
            global $post;
            if ( '' == $text ) {
                    $text = get_the_content('');
                    $text = apply_filters('the_content', $text);
                    $text = str_replace('\]\]\>', ']]>', $text);
                    $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                    $text = strip_tags($text, '<a>');
                    $excerpt_length = 80;
                    $words = explode(' ', $text, $excerpt_length + 1);
                    if (count($words)> $excerpt_length) {
                            array_pop($words);
                            array_push($words, '</a><a>ID) . '">Read the Rest...</a>');
                            $text = implode(' ', $words);
                    }
            }
            return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'new_trim_excerpt');

Topic Closed

This topic has been closed to new replies.

About this Topic