Support » Developing with WordPress » Trim the_excerpt

  • Does anyone know how I might get rid of breaks/newlines from the_excerpt? I want to configure a digg badge for each of my posts, and the code looks like this:

    <script type="text/javascript">
    digg_url = '<?php the_permalink(); ?>';
    digg_title = '<?php the_title(); ?>';
    digg_bodytext = '<?php the_excerpt(); ?>';
    </script>
    <script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>

    The only problem, however, is that the_excerpt for some posts returns a string with a new line. Thus, the javascript thinks that there is an error, an unterminated string literal. I tried digg_bodytext = '<?php trim(the_excerpt()); ?>'; but that didn’t work.

    Any suggestions? (And sorry if this is the wrong section…I wasn’t sure if it was a “hack” or something “advanced” – it’s not really any of them.)

Viewing 5 replies - 1 through 5 (of 5 total)
  • Use

    digg_bodytext = '<?php echo trim(get_the_excerpt()); ?>';

    Let me know if it works

    I’d try something more rigorous than just trim() here:

    digg_bodytext = '<?php echo trim(preg_replace('/\s/', ' ', get_the_excerpt())); ?>';

    Let me bring this thread back up to the surface 🙂

    Kafkaesqui’s solution helped me a lot, since I wanted to show the excerpt, if there is one, and the full content if there is no excerpt.

    Which looks like

    <?php if (trim(preg_replace('/\s/', ' ', get_the_excerpt()))){the_excerpt('more »');} else {the_content('more »');} ?>

    Now, my problem is that if I have a post that has an excerpt, and then remove the excerpt, I do not get the full version (the_content), but some other sort of abbreviated content.

    EDIT: Whoa, hold your horses, it now suddenly does work. No idea what I did, maybe I had to change the content text of the post too …

    marcvdwurff

    (@marcvdwurff)

    shouldn’t it be \n? \s leaves me with questionmarks

    nathanrice

    (@nathanrice)

    how about using the_excerpt_rss with the trip and preg_replace methods above?
    http://codex.wordpress.org/Template_Tags/the_excerpt_rss

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Trim the_excerpt’ is closed to new replies.