Support » Themes and Templates » Showing just the first X words of the_content()

  • Resolved buskerdog

    (@buskerdog)


    I’m using the excerpt for something else and in my archive.php I’d like to show just the first, say, 100 characters of my content. How can I do this?

Viewing 10 replies - 1 through 10 (of 10 total)
  • $content = get_the_content();
    echo substr($content, 0, 100);

    it should works
    constructed from information from http://codex.wordpress.org/Template_Tags/the_content and http://pl.php.net/manual/pl/function.substr.php

    Pretty crafty. Any idea how to get it to strip out <img src>s and <a href>s?

    i.e. do to it something similar to what the excerpt function does.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    $content = strip_tags($content); will strip the html out of the content before you do the character thing.

    I’m looking through post-template.php for a clue as to what I can do to $content to strip that stuff out but I’m not finding it. Thoughts?

    heh should have refreshed. I had a feeling it was strip_tags but for some reason I was trying everything else first. Thanks!

    If anyone finds this thread later trying to do what I wanted to do here’s how it finally looks:

    <div class="entry">
    <?php $content = get_the_content();
          $content = strip_tags($content);
          echo substr($content, 0, 255);
    ?>…<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">click for more...</a>
    </div>

    The whole point of this was to free up my excerpt for something else.

    As an alternative i think you could proberly create a second excerpt function in the theme functions.php.

    Yeah, I thought about that. I’m trying to keep my customization to my themes as much as possible to prevent upgrades from becoming a mess.

    Example posted in this thread….

    Here

    You would of course not need the remove_filter part….

    The code posted is a replacement, but it could be used as a secondary function to…

    Mark

    (@encryptdesigns)

    The only problem with that then is if you upgrade your WP install then your changes would probably get overwritten!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Showing just the first X words of the_content()’ is closed to new replies.