WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Showing just the first X words of the_content() (11 posts)

  1. buskerdog
    Member
    Posted 5 years ago #

    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?

  2. kkarpieszuk
    Member
    Posted 5 years ago #

    $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

  3. buskerdog
    Member
    Posted 5 years ago #

    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.

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

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

  5. buskerdog
    Member
    Posted 5 years ago #

    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?

  6. buskerdog
    Member
    Posted 5 years ago #

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

  7. buskerdog
    Member
    Posted 5 years ago #

    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.

  8. t31os
    Member
    Posted 5 years ago #

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

  9. buskerdog
    Member
    Posted 5 years ago #

    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.

  10. t31os
    Member
    Posted 5 years ago #

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

  11. Mark
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic