Php 循环帖子摘要返回空
我以这种方式显示我的帖子列表:Php 循环帖子摘要返回空,php,wordpress,Php,Wordpress,我以这种方式显示我的帖子列表: [-- IMAGE --] [-- Title --] [-- Excerpt --] [-- "Read more" --] 当我在index.php文件中使用它时,我的代码运行良好,比如: while(have_posts()) { the_post(); $link = get_permalink(); $title = the_title(); $excerpt = the_excerpt(); } 然后在functi
[-- IMAGE --]
[-- Title --]
[-- Excerpt --]
[-- "Read more" --]
当我在index.php
文件中使用它时,我的代码运行良好,比如:
while(have_posts())
{
the_post();
$link = get_permalink();
$title = the_title();
$excerpt = the_excerpt();
}
然后在functions.php
中,我对摘录应用了一个过滤器:
add_filter('excerpt_length', 'custom_excerpt_length');
add_filter('excerpt_more', 'new_excerpt_more');
function custom_excerpt_length()
{
return 14;
}
function new_excerpt_more($more)
{
global $post;
return '... <a href="'. get_permalink($post->ID) . '">Read more</a>';
}
如何将过滤器应用于函数“获取摘录”以及为什么它是空的?来自:
如果此函数在循环外部使用,并且post没有
自定义摘录,此函数将用于生成
摘录。该函数使用,必须使用
使用循环,并且如果正在获取\u摘录(),则会导致问题
在循环外使用。为了避免这些问题,请使用
在调用get_以设置
全局$post对象
这应该起作用:
$posts = get_posts($args);
foreach($posts as $post)
{
setup_postdata($post);
$link = get_permalink($post);
$title = get_the_title($post);
$excerpt = get_the_excerpt($post);
}
wp_reset_postdata();
$posts = get_posts($args);
foreach($posts as $post)
{
setup_postdata($post);
$link = get_permalink($post);
$title = get_the_title($post);
$excerpt = get_the_excerpt($post);
}
wp_reset_postdata();