主页上的帖子显示其他帖子的摘录

时间:2013-11-22 22:15:19

标签: php wordpress

我遇到了一个奇怪的问题。我创建了一个自定义主页模板,将来自两个不同类别的最新5个帖子拉到布局中(请参阅“学习和”播放“列)。

http://sensoryrevolution.com.s177767.gridserver.com/

我通过我从这个网站复制+粘贴的自定义查询提取了4个字段:

  • 发布缩略图
  • 永久
  • 帖子标题
  • 摘录

只有摘录错了。它显示了上面的博客文章的摘录。

这是我用来引入它的代码。奇怪的是它正在工作,现在它已经坏了。我已经退出了一些其他自定义代码,这些代码限制了摘录中的字符数,所以这纯粹是the_excerpt()正在发挥作用。

<?php $posts = get_posts('category=213&orderby=desc&numberposts=5'); foreach($posts as $post) { ?>
    <h4><a href="<?php the_permalink() ?>" target="_parent"><?php the_title(); ?></a></h4>
    <a href="<?php the_permalink() ?>" target="_parent"><?php the_post_thumbnail('blog-large'); ?></a>
    <p><?php the_excerpt() ?><?php // echo excerpt(22) ?></p>
<?php } ?>

这让我抓狂!有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

post excerpt摘录了wordpress中的摘录字段。默认情况下,摘要字段不显示在管理员后端的wordpress单个帖子/页面上。您可以通过单击“添加新”帖子/页面(或自定义页面)右上角的“屏幕选项”来添加它。然后,您可以输入您想要的任何文本作为摘录。

正如你在var_dump中看到的那样,帖子中没有摘录:

" ["post_title"]=> string(14) "It Makes Sense" ["post_excerpt"]=> string(0)

如前所述,the_excerpt()不会在主要内容正文中查找摘录。如果您不想在后端添加摘录字段,可以执行以下操作:

$content = get_the_content(); echo substr($content, 0, 20);

这将输出字符串的前20个字符(这是页面/帖子/自定义帖子的内容)。显然你也可以做40或类似的事情,以适当地输出你认为的礼仪金额。

快乐的编码!