我正在尝试使用get\\u the\\u摘录获取一篇博客文章的摘录,每次都会返回空白,我无法找出我做错了什么。这是我得到的:
模板:
<div class="col-sm-12">
<?php $blogPost1 = wp_get_recent_posts( array(
\'numberposts\' => 1,
\'category\' => 42,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\'
) ); ?>
<h1><?php echo $blogPost1[0][\'post_title\']; ?></h1>
<br>
<?php echo get_the_excerpt( $blogPost1[0][\'ID\'] ); ?>
<br>
</div>
我的内容:
https://youtu.be/BF2Ksrxu_QY
<div></div>
<div>Ok, so I’m supposed to be writing a review for this month’s film review- but I’m genuinely struggling to keep a straight face, let alone to not just crack up laughing. ‘Why?’, you may ask; well, the reason is: this month’s film is <i>The Snowman</i>, and I don’t think I’ve laughed so hard at something so unintentionally funny, in such a long time. Yes, I know what you’re thinking- but isn’t <i>The Snowman</i> that new crime thriller film with what’s-his-name-<i>Fassbender</i> in it? And the answer is yes, it is- but I think the filmmakers have missed a trick here, it really should’ve just been made as an intentional comedy!</div>
<div></div>
<!--more-->
<div>It is safe to say that when I heard Swedish Director, <i>Tomas Alfredson</i> (who has made such greats as: <i>Tinker Tailor Soldier Spy</i> and <i>Let The Right One In</i>), was going to be taking the helm of this project, I was excited to watch the finished product (despite the fact that <i>Martin Scorsese</i> had been the original Director on board); however the Crime novel, penned by Norwegian writer, <i>Jo Nesbø</i> (which also happens to be the 7th book in the ‘Detective Harry Hole’ series), was not in safe hands- as it turns out.</div>
我不确定我做错了什么,或者我应该做什么。
SO网友:Jacob Peattie
如果帖子没有手动摘录(如添加到帖子编辑屏幕上的摘录框中),则get_the_excerpt()
习惯于wp_trim_excerpt()
生成摘录。问题是wp_trim_excerpt()
使用全局$post
对象,该对象在循环中设置,而不考虑传递给的任何IDget_the_excerpt()
.
So使用get_the_excerpt()
在需要使用的循环之外setup_postdata()
设置全局$post
至所需职位:
<?php
$blogPost1 = wp_get_recent_posts( array(
\'numberposts\' => 1,
\'category\' => 42,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\'
) );
global $post;
$post = $blogPost1[0]; // Post must be assigned to the global $post variable.
setup_postdata( $post );
?>
<!-- Now you can use template tags without specifying the ID. -->
<h1><?php the_title(); ?></h1>
<br>
<?php the_excerpt(); ?>
<br>
<?php wp_reset_postdata(); ?>
但是,根据您提供的内容,您正在使用
<!--more-->
标记,因此您实际上不需要摘录。仅显示需要使用的“更多”标记上方的内容
the_content()
.
the_content()
这样,more标记上方的内容仅显示在非单数页上,而全文则显示在单页上。请注意,此行为与
the_content()
是否在主查询中。要强制它仅显示more标记上方的内容,您需要使用全局
$more
变量:
setup_postdata( $post );
global $more;
$original_more = $more;
$more = 0;
?>
<h1><?php the_title(); ?></h1>
<br>
<?php the_content(); ?>
<br>
<?php
wp_reset_postdata();
$more = $original_more;