我只是安装Wordpress SEO插件Yoast,测试标题重写功能,它在我的标题栏(单篇文章)工作。然而,当我尝试在widget中调用标题时,我只是得到错误消息
警告:kpndg()…缺少参数1
我将这段代码插入到主题函数。php
add_filter( 'wpseo_title', 'kpndg' );
function kpndg( $title ) {
return $title;
}
然后在我的widget中调用它:
$out.='<div class="trending-bar add-active bar-' . $i . '">';
$out.='<a class="trending-link" href="'.get_permalink().'"> </a>';
$out.='<div class="title">'.kpndg().'</div>';
$out.='<div class="trending-color-wrapper">';
$out.='<div class="trending-color-layer"></div>';
$out.='<div class="trending-color"></div>';
$out.='<div class="trending-meta">' . $meta . '</div>';
$out.='</div>';
$out.='</div>';
我不知道这段代码有什么问题。
供参考:我使用引擎主题的工业主题
这一行:$out.='<div class="title">'.kpndg().'</div>';
你需要指定一个参数…或者,为$title
参数设置一个默认值:
add_filter( 'wpseo_title', 'kpndg' );
function kpndg( $title = 'foo' ) {
return $title;
}
编辑
你确定你不是有意用get_post_meta(get_the_ID(), '_yoast_wpseo_title', true);
代替kpndg()
吗?
编辑# 2
不需要为wpseo_title
添加新的过滤器,只需创建一个"辅助函数":
function kpndg() {
return get_post_meta(get_the_ID(), '_yoast_wpseo_title', true) ?: get_the_title();
}
然后,在循环中,您可以调用echo kpndg()