0

I'm having a heck of a time with this! I'm trying to force this page to only show a limited amount of words regardless if they insert a readmore tag.

I was going to use the_excerpt, but it doesn't add a readmore link at the end of the excerpt.

I have my index page pulling my blog roll by using this code:

<div class="entry-content">
    <?php
        /* translators: %s: Name of current post */
        the_content( sprintf(
            __( 'more %s <span class="meta-nav">...</span>', 'gateway' ),
            the_title( '<span class="screen-reader-text">"', '"</span>', false )
        ) );
    ?>
</div>

In my reading settings I have set "For each article in a feed, show" to "summary".

So I guess my question is this: Is there a away to limit the_content() or alternatively add a read more to the_excerpt()?

2 Answers 2

4

Try wp_trim_words() https://codex.wordpress.org/Function_Reference/wp_trim_words

You won't be able to use it with the_content() though because it echoes the content. You'll want to use it with get_the_content() which just returns the info.

So it'd look something like this

echo wp_trim_words( get_the_content(), $num_words, $more_text );

Edit:

It's good to note that the_content and get_the_content will return/echo any HTML that is in your content, while the_excerpt or get_the_excerpt will return only the text. So use the one that best fits your needs on how many words you want to return and whether or not you care to include HTML (such as images or embeded videos) in your output.

3
  • Ian, thanks for your help! I tried that and even tried this: echo wp_trim_words( get_the_content(), 40, '<a href="' . esc_url( get_permalink() ) . '">MORE</a>'); but it won't load the link and just keeps saying (more...) any ideas?
    – rudtek
    Commented Mar 16, 2017 at 19:10
  • Try replacing get_permalink() with get_the_permalink() and see if that does the trick :) If it still doesn't work, try removing the esc_url wrapper to see if that is causing the issue.
    – Ian
    Commented Mar 16, 2017 at 19:42
  • ian, that a tleast put the link, but it doesn't quite work. if the post is less than 40 words or if the more tag in the post limits the excerpt to less than 40 words, it uses the link in in the get_the_content. But outside above it add the the link together. so i would get permalink().'more'. which leads to a 404 error. i went simple because I couldn't figure it out. I'm giving your answer an upvote for trying though. I really appreciate it.
    – rudtek
    Commented Mar 18, 2017 at 17:25
0

I couldn't get this resolved with the_content() so i went simple and this works:

        the_excerpt();
        echo '<a href="' . esc_url( get_the_permalink() ) . '"> more...</a>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.