2011-07-02 57 views
1

我试图限制get_the_content的字符串输出,但我不能对如何做到这一点的净任何地方。如何限制wordpress上get_the_content()的字数?

一切我找到的是关于the_content()。因为我希望字符串是格式化的,我没有使用the_content

,因为某种原因,它似乎并没有在我的右环行工作了我的一切职务。

不管怎么说,没有人没有如何使get_the_content回报只有实际的描述指定的字符数?我不想诉诸使用摘录,因为这是保留给我使用的其他信息。

+0

你找到'the_content'什么解决办法吗?将其调整为'get_the_content'应该很简单。 – Dogbert

+0

有人会想,但不会,因为某种原因,他们似乎以不同的方式工作。我无法理解的是,为什么wordpress codex中这么少呢。我不会在这里发布一个问题,如果这很容易。 – Robert

+0

我只是问你如何修改'the_content'的输出。我知道get_the_content在返回之前不会在'the_content'上应用过滤器,所以大多数WP的过滤器(如自动段落,自动智能引用)都不会被添加。 – Dogbert

回答

2

我没有测试过这一点,但我认为它会运行

转到WP-包括/-的template.php后

找到get_the_content()函数

在结束的功能,有

return $output; 

是最后一行之前添加

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

所以你留下了

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

return $output; 

你想改变在代码中的数字“13”以上的部分 - 只是把你想显示

的单词数让我知道它是如何工作的,你

0

尝试SUBSTR(),

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

是的,但是,如果描述有2000个字符,并且只需要200个字符,那么您将从数据库传输10倍的数据,然后实际需要。 –

0

尝试。它的工作我的

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

(更改50到你的愿望的话长度)