开发者

How can I limit the word count for get_the_content() on wordpress?

开发者 https://www.devze.com 2023-03-17 01:48 出处:网络
I\'m trying to limit the string output of get_the_content but I can\'t find anywhere on the net on how to do this.

I'm trying to limit the string output of get_the_content but I can't find anywhere on the net on how to do this.

Everything I find is regarding the_content().

I'm not using the_content because I want the string to be unformatted and because for some reason it doesn't seem to work right on my loop for all the posts 开发者_如何学PythonI have.

Anyways, does anyone no how to make get_the_content return only a specified number of characters of the actual description? I don't want to resort to using the excerpt as that is reserved for some other information I'm using.


I haven't tested this but I think it will work..

Go to wp-includes/post-template.php

Find the get_the_content() function

At the end of the function, there is

return $output;

Before that final line, add

$output = preg_replace("/((\S+\s+){1,13}).*/s","\\1",strip_tags($output));

So you're left with

    $output = preg_replace("/((\S+\s+){1,13}).*/s","\\1",strip_tags($output));

return $output;

The part you'll want to change is the number "13" in the code above - just put the number of words you want to display

Let me know how that works for you


try this. It worked of me

<?php $mycontent = get_the_content();
  $trimmed_content = wp_trim_words( $mycontent , 50, '<a href="'. get_permalink() .'">...[ read more ]</a>' ); ?>
  <p><?php echo $trimmed_content; ?></p>

(Change the 50 to your desire words length)


try substr(),

$description = substr(get_the_content(), 0, $number_of_characters);
0

精彩评论

暂无评论...
验证码 换一张
取 消