【问题标题】:WordPress Coding And Content ExcerptWordPress 编码和内容摘录
【发布时间】:2013-02-05 07:57:06
【问题描述】:

所以我写了一个函数来处理摘录,但只用于首页。

http://stevefleming.co.uk/

而函数是……

function excerpt_filter2($limit) {

$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", "", $content);
$excerpt = explode(' ', $content, $limit);
if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt) . "...&nbsp;<a href='". get_permalink(the_ID()) ."'> continue reading</a>";
} else {
    $excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
echo $excerpt;
}

正如您在首页看到的那样,问题在于它不断将帖子 ID 附加到文本的前面。

我尝试调试代码并在调用 implode 之前转储 $excerpt 数组,以检查 id 是否以某种方式放置在数组中......它不是。

我不知道帖子 ID 是如何到达那里的。

有什么想法吗?

史蒂夫

【问题讨论】:

  • the_ID -> get_the_ID

标签: php wordpress


【解决方案1】:

函数the_ID() 自动输出结果。更改此行:

$excerpt = implode(" ",$excerpt) . "...&nbsp;<a href='". get_permalink(the_ID()) ."'> continue reading</a>";

收件人:

$excerpt = implode(" ",$excerpt) . "...&nbsp;<a href='". get_permalink(get_the_ID()) ."'> continue reading</a>";

查看the codex 了解更多信息,Wordpress 通常有一个替代函数,它不会以以下形式输出结果:get_*

【讨论】:

  • 谢谢!我知道这会很简单:)
【解决方案2】:

你的代码在这里

$excerpt = implode(" ",$excerpt) . "...&nbsp;<a href='". get_permalink(the_ID()) ."'> continue reading</a>";

你正在使用

the_ID()

这将与帖子的 id 一致,因此请尝试将其更改为

get_the_ID()

希望这就是您所面临的。 :) 干杯

【讨论】:

  • 感谢您的回复,汤姆刚刚将您带到了帖子中,但我非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多