我目前正在创建一个短代码,以便在我的模板中将自定义分类术语显示为列表:
// First we create a function
function list_terms_forme_juridique_taxonomy( $atts ) {
// Inside the function we extract custom taxonomy parameter of our
shortcode
extract( shortcode_atts( array(
'custom_taxonomy' => 'forme_juridique',
),
$atts ) );
// arguments for function wp_list_categories
$args = array(
taxonomy => $custom_taxonomy,
title_li => ''
);
// We wrap it in unordered list
echo '<ul>';
echo wp_list_categories($args);
echo '</ul>';
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
我遇到了以下 2 个问题:
- 短代码(渲染)显示在我的页面顶部,而不是我将其放置在页面中的位置;
- PHP 控制台标记以下 2 个错误:
- 使用未定义的常量分类法 - 假定的“分类法”
- 使用未定义的常量 title_li - 假定为“title_li”
任何帮助表示赞赏!
谢谢
最佳答案
首先,您的短代码的输出显示在页面顶部,因为您正在回显输出。您应该创建一个 $output 变量并用您想要显示的内容构建它,然后返回它。例如:
$output = '';
$output .= '<ul>';
$output .= wp_list_categories($args);
$output .= '</ul>';
return $output;
其次,您会收到错误,因为您没有在数组声明中引用键。因此 PHP 假定它们应该是先前定义的常量。
$args = array(
taxonomy => $custom_taxonomy,
title_li => ''
);
应该是:
$args = array(
'taxonomy' => $custom_taxonomy,
'title_li' => ''
);
关于wordpress - 简码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50946170/