如何获得没有摘录的帖子的前几个词?
$args = array(
'posts_per_page'=>
5, 'offset'=>
0, 'category'=>
'', 'category_name'=>
'Energy', 'orderby'=>
'date', 'order'=>
'DESC', 'include'=>
'', 'exclude'=>
'', 'meta_key'=>
'', 'meta_value'=>
'', 'post_type'=>
'post', 'post_mime_type'=>
'', 'post_parent'=>
'', 'author'=>
'', 'author_name'=>
'', 'post_status'=>
'publish', 'suppress_filters' =>
true
);
$posts_array = get_posts( $args );
foreach($posts_array as $postCategory)
{
echo get_the_title( $postCategory->
ID );
echo get_permalink( $postCategory->
ID );
echo get_the_excerpt( $postCategory->
ID );
// THIS WITH OU WITHOUT EXCERPT DOES NOT WORK
}
该代码正在打印标题和固定链接, 但不打印帖子的前几个单词。
我想了解如何打印帖子的前几个单词, 无论它是否具有摘录分隔符。
谢谢
#1
$trimtitle = get_the_content($post->
ID);
$shorttitle = wp_trim_words( $trimtitle, $num_words = 15, $more = '… ' );
?>
<
?php echo $shorttitle;
?>
#2为此, 你需要添加以下功能:
function truncate_string ($string, $maxlength) {// Set the replacement for the "string break" in the wordwrap function
$cutmarker = "**cut_here**";
// Checking if the given string is longer than $maxlength
if (strlen($string) >
$maxlength) {// Using wordwrap() to set the cutmarker
// NOTE: wordwrap (PHP 4 >
= 4.0.2, PHP 5)
$string = wordwrap($string, $maxlength, $cutmarker);
// Exploding the string at the cutmarker, set by wordwrap()
$string = explode($cutmarker, $string);
}// returning $string
return $string;
}
然后在你的foreach循环中添加以下内容:
$content_post = get_post( $postCategory->
ID );
$content = $content_post->
post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>
', ']]&
gt;
', $content);
echo truncate_string( $content, 75 );
【我如何获取帖子的几个词】(将75更改为你想要的任何长度)
推荐阅读
- 你如何在不停机的情况下将大量内容传输到新的WordPress主题()
- 如何在WordPress中将变量传递给模板部分()
- 如何在WordPress中制作此自定义菜单
- 求一文本编辑器控件
- golangbinary包
- 2017.7.15
- 看云识天气
- Vista error icons教程翻译
- 今日偶感 - 成功之于我们