Forums

How to remove the [...] from the excerpt (25 posts)

  1. rusting1927
    Member
    Posted 3 years ago #

    Hi,
    Instead of using the_content() and other codes, i have used the_excerpt() code for the posts to be cut off on the home page. However i am now finding that at the point where the article is cut off, i get a [...] sign. Is there any way that i can remove it?

  2. Chrisber
    Member
    Posted 3 years ago #

    Do you want it to be gone, or do you want something else in its place? You could use the more tag instead.

  3. rusting1927
    Member
    Posted 3 years ago #

    I want it to be gone...i want to completely remove it

  4. visiblemedia
    Member
    Posted 2 years ago #

    I'm having this issue too, I'd like to remove the <p> that comes after that too so that the read more link immediately follows the excerpt without dropping down a line.

  5. visiblemedia
    Member
    Posted 2 years ago #

    ok, if anyone here is interested in the <p></p> solution, i found it:

    <?php remove_filter('the_excerpt', 'wpautop'); ?>

    that gets rid of the <p></p> now if i can only get rid of the [...]

    :)

  6. t31os
    Member
    Posted 2 years ago #

    You can just re-write the excerpt function in your theme functions file...

    Like this...

    function wp_new_excerpt($text)
    {
    	if ($text == '')
    	{
    		$text = get_the_content('');
    		$text = strip_shortcodes( $text );
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text);
    		$text = nl2br($text);
    		$excerpt_length = apply_filters('excerpt_length', 55);
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words) > $excerpt_length) {
    			array_pop($words);
    			array_push($words, '[...]');
    			$text = implode(' ', $words);
    		}
    	}
    	return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'wp_new_excerpt');

    Just change this line...

    array_push($words, '[...]');

    You can also change the character limit here...

    $excerpt_length = apply_filters('excerpt_length', 55);

  7. visiblemedia
    Member
    Posted 2 years ago #

    hm... I tried that but my functions file doesn't have that in it. so I added it verbatim like this:

    <?php
    function wp_new_excerpt($text)
    {
    if ($text == '')
    {
    $text = get_the_content('');
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $text = nl2br($text);
    $excerpt_length = apply_filters('excerpt_length', 55);
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '...');
    $text = implode(' ', $words);
    }
    }
    return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'wp_new_excerpt');
    ?>

    I just changed

    array_push($words, '[...]');

    to

    array_push($words, '...');

    and it kinda worked on the page, but something else in the above code interfered with publishing. I got an error having to do with functions.php and a header already being called or something like that.

    so I took it out. :(

    Is there a way to add JUST the array thing?

    I assume this

    <?php
    function wp_new_excerpt($text)
    array_push($words, '...');
    ?>

    isn't right, and I'm afraid to try it.

  8. apljdi
    Member
    Posted 2 years ago #

    Why not this?

    function trim_excerpt($text) {
      return rtrim($text,'[...]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');

    I tested it in my function.php and it works like a charm. Maybe I'm missing something?

    Of course, my code removes everything ( and it doesn't deal with the <p> problem above ) but you could add back anything you want by concatenating the string like so: `
    return rtrim($text,'[...]').'whateveryouwantgoeshere';`

    It is also possible to do the same with str_replace or preg_replace instead of rtrim but I'm not sure the extra complexity is worth it.

    Oh, and visiblemedia, you aren't inserting into the database here so it isn't really possible to do any real damage. This filters only works on the displayed text. It doesn't alter the information in the database. About the worst that can happen is that you get a screwy page view for a few minutes.

  9. t31os
    Member
    Posted 2 years ago #

    No the function won't exist in your themes function file, that was the point, the example was one you can add in, as you have done..

    It's the same code i use on my site except i changed the function name to something more obvious for the example...

    I still use [...], but i just tested it with ... and it worked no problem...

    You sure you didn't delete a ' by accident or something silly when testing?

    Of course the above solution may be better suited in this case, the example provided by myself will give you a bit more control over the rest of the excerpt function though, if you need it... such as character limit.

  10. apljdi
    Member
    Posted 2 years ago #

    Confession: I'm having second thought about my function. I may have tested it improperly. I'll have to fiddle with it more later.

  11. apljdi
    Member
    Posted 2 years ago #

    Ok I tested it a bit more and it still seems to work.

  12. visiblemedia
    Member
    Posted 2 years ago #

    hm... well that didn't work, my exerpts ended up looking like this:

    Last Chance for Freebie Play
    February 28, 2009 | By RotoRob | Comments (0)

    Warning: rtrim() [function.rtrim]: Invalid '..'-range, no character to the left of '..'. in /nfs/c04/h01/mnt/60416/domains/rotorob.com/html/wp-content/themes/RotoRob_v7/functions.php on line 32

    Warning: rtrim() [function.rtrim]: Invalid '..'-range, no character to the right of '..'. in /nfs/c04/h01/mnt/60416/domains/rotorob.com/html/wp-content/themes/RotoRob_v7/functions.php on line 32
    If you don’t want your kid to wind up like this, teach him how to play fantasy sports properly. In the past couple of weeks, we’ve brought you articles designed to help you with your strategy when playing hockey or basketball fantasy leagues on Draftbug. Well, today (i.e., February 28) is your last chance to take advantage [...] more

    I wonder if the stuff in the template is conflicting with the stuff in the functions.php?

    I have this in my template:

    <?php $count++; ?>
    <?php if ($count < 2) : ?>
    <?php the_content() ?>
    <?php else : ?>
    <?php remove_filter('the_excerpt', 'wpautop'); ?>
    <?php the_excerpt();?>
    <a href="<?php the_permalink() ?>">more</a>
    <?php endif; ?>

    where you would normaly find this:

    <?php the_content() ?>

    I do that so that I can display the first post in full, followed by 4 excerpts.

    maybe the:

    <?php remove_filter('the_excerpt', 'wpautop'); ?>
    <?php the_excerpt();?>

    in the template is conflicting with the

    function trim_excerpt($text) {
      return rtrim($text,'[...]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');

    in the functions.php?

  13. apljdi
    Member
    Posted 2 years ago #

    Well, you don't want to be trying multiple solutions at the same time. Looks like t31os suggested a way to remove the '[...]' and I suggested a way to remove it. You decided to try remove_filter('the_excerpt', 'wpautop'); in order to remove the paragraph tags. That is three separate solutions, or attempts at solutions. Try them one at a time, at least at first. Then mix and match carefully.

    What exactly is on line 32 of your functions file? And what software did you use to edit the file?

  14. visiblemedia
    Member
    Posted 2 years ago #

    hmm, I commented out:

    <?php remove_filter('the_excerpt', 'wpautop'); ?>

    from the template files (archive.php and index.php) and added:

    function trim_excerpt($text) {
      return rtrim($text,'[...]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');

    back into the functions.php file and got the same kind of error.

  15. visiblemedia
    Member
    Posted 2 years ago #

    then I tried adding this:

    <?php function trim_excerpt($text) { return rtrim($text,'[...]'); } ?>

    to this:

    <?php $count++; ?>
    <?php if ($count < 2) : ?>
    <?php the_content() ?>
    <?php else : ?>
    <?php remove_filter('the_excerpt', 'wpautop'); ?>
    <?php the_excerpt();?>
    <a href="<?php the_permalink() ?>">more</a>
    <?php endif; ?>

    so that it looks like this:

    <?php $count++; ?>
    <?php if ($count < 2) : ?>
    <?php the_content() ?>
    <?php else : ?>
    <?php remove_filter('the_excerpt', 'wpautop'); ?>
    <?php function trim_excerpt($text) { return rtrim($text,'[...]'); } ?>
    <?php the_excerpt();?>
    <a href="<?php the_permalink() ?>">more</a>
    <?php endif; ?>

    in the index.php and archive.php file and got all sorts of other errors... something about "cannot re declare the_exerpt" or something like that. and my sidebar disappeared.

  16. visiblemedia
    Member
    Posted 2 years ago #

    ooops, double post by accident, sorry

  17. apljdi
    Member
    Posted 2 years ago #

    By 'same kind of error' you mean 'Warning: rtrim() [function.rtrim]: Invalid '..'-range'? I can't duplicate that. It has something to do with using double periods as a range operator in rtrim but I still can't get rtrim to choke.

    Is t31os's function still in your functions file? That might explain the 'redeclare' error and it might explain some of the other errors. The different solutions may not be compatible.

    Adding the trim_excerpt function the way you did is useless. The function never gets called. That is, it never executes. If however, you called it elsewhere and before it gets defined you could get errors.

  18. visiblemedia
    Member
    Posted 2 years ago #

    Nope t31os's function is not in my functions file anymore, this is what I have now in my functions file:

    <?php
    if ( function_exists('register_sidebars') )
     register_sidebars(2,array(
            'before_widget' => '',
        	'after_widget' => '',
     		'before_title' => '<h2>',
            'after_title' => '</h2>',
        ));
    ?>
    <?php
    function widget_mytheme_search() {
    ?>
    <h2>Search</h2>
    <form id="searchform" method="get" action="<?php bloginfo('home'); ?>/"> <input type="text" value="type, hit enter" onfocus="if (this.value == 'type, hit enter') {this.value = '';}" onblur="if (this.value == '') {this.value = 'type, hit enter';}" size="18" maxlength="50" name="s" id="s" /> </form>
    <?php
    }
    if ( function_exists('register_sidebar_widget') )
        register_sidebar_widget(__('Search'), 'widget_mytheme_search');
    ?>
    <?php
    add_filter('comments_template', 'legacy_comments');
    function legacy_comments($file) {
    	if(!function_exists('wp_list_comments')) : // WP 2.7-only check
    		$file = TEMPLATEPATH . '/legacy.comments.php';
    	endif;
    	return $file;
    }
    ?>

    and this is what I have in my archive and index templates:

    <div class="entry">
    <?php $count++; ?>
    <?php if ($count < 2) : ?>
    <?php the_content() ?>
    <?php else : ?>
    <?php remove_filter('the_excerpt', 'wpautop'); ?>
    <?php the_excerpt();?>
    <a href="<?php the_permalink() ?>">more</a>
    <?php endif; ?>
    </div>
  19. Roy
    Member
    Posted 2 years ago #

    Instead of using the_excerpt I just use the Excerpt Editor plugin. It makes "continue reading" links like the more tag.
    ...

  20. apljdi
    Member
    Posted 2 years ago #

    You also don't have my function in there anywhere. So what is supposed to be removing the '[...]'?

  21. visiblemedia
    Member
    Posted 2 years ago #

    apljdi: I took your function out because it conflicted with what I already have. I posted what I already have in hopes that you can guide me as to how to implement the trim so as not to conflict with what I already have. Can you help?

  22. apljdi
    Member
    Posted 2 years ago #

    It looks like you are running remove_filter('the_excerpt', 'wpautop') on every loop. I'd move that out of the loop and save some processor load.

    Try this and see if you get a conflict:

    function trim_excerpt($text) {
      return rtrim($text,'[.]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');
  23. visiblemedia
    Member
    Posted 2 years ago #

    Actually, I already tried that. This is the post (cut n' paste from previous post) with what happened when I did:

    <?php remove_filter('the_excerpt', 'wpautop'); ?>

    from the template files (archive.php and index.php) and added:

    function trim_excerpt($text) {
      return rtrim($text,'[...]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');

    back into the functions.php file and got the same kind of error.

  24. Elpie
    Member
    Posted 2 years ago #

    If you simply want to remove the [...] and replace it with something like "read more" (or whatever you want), just do this...

    //function to replace invalid ellipsis with text linking to the post
    function elpie_excerpt($text)
    {
       return str_replace('[...]', '<a href="'. get_permalink($post->ID) . '">' . '[Read More &hellip;]' . '</a>', $text);
    }
    add_filter('the_excerpt', 'elpie_excerpt');

    You can add a line break too, if you want. I've explained it all here: http://lynnepope.net/the-excerpt-revisited

  25. t31os
    Member
    Posted 2 years ago #

    You could also add custom field support to that function...

    A sort of 'if field exists and has a value, take this value and use it as the replacement text, if not then use a default'...

    That way you could make it a little more flexible and modify the output on a per post basis...

    Of course that may be of no use to anyone, but if it is, i don't mind adding on to what's been posted...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.