【问题标题】:Excerpt in templates with single post带有单个帖子的模板中的摘录
【发布时间】:2017-10-25 22:13:45
【问题描述】:

在主题中,我在index.php 中使用get_the_excerpt,在其中我使用循环来显示多个帖子的标题、摘录……并且它按预期工作。摘录/预告片手动标记了“阅读更多”标签。

但是,我也想在single.php 中使用get_the_excerpt,但我得到的不是只接收到“阅读更多”标签的文本,而是前 55 个单词。

documentation 说:

<!--more--> 快速标签将不起作用,并且在仅显示一篇文章的模板中被忽略,例如 single.php。

好的,如果我希望摘录仅包含直到 single.php 中的快速标签的文本,我该怎么办?

【问题讨论】:

  • 只是好奇您是否尝试过您链接到的页面上第一个用户提供的注释中提供的示例?带有global $more; $more = 0; ....的那个?
  • @cale_b 我​​的老眼睛不像以前那样了。如果您将其发布为答案,我会接受它,谢谢!

标签: wordpress


【解决方案1】:

我相信您可以使用 WordPress 的内置功能,利用 $more 变量来完成您想要的。

为了在更多的快速标签之前显示内容,您应该能够在您的 single.php 上执行此操作:

// Declare global $more (before the loop).
global $more;

while( have_posts() ) {
   // Set (inside the loop) to display content above the more tag.
   $more = 0;
   // call the_post() to prepare post variables / functions
   the_post();
   // output your markup, post title, etc...
   // then this the_content() should render only the content before the more quicktag
   the_content( 'More ...' );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多