WordPress.org

Ready to get started?Download WordPress

Forums

Trim the_excerpt (6 posts)

  1. mildlyhotpeppers
    Member
    Posted 6 years ago #

    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.)

  2. Sudar
    Member
    Posted 6 years ago #

    Use

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

    Let me know if it works

  3. Kafkaesqui

    Posted 6 years ago #

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

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

  4. mores
    Member
    Posted 5 years ago #

    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 ...

  5. marcvdwurff
    Member
    Posted 5 years ago #

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

  6. nathanrice
    Member
    Posted 5 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic