wordpress - 简码问题

标签 wordpress shortcode wordpress-shortcode

我目前正在创建一个短代码,以便在我的模板中将自定义分类术语显示为列表:

// 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/

相关文章:

php - 简码内容 wordpress 中的 p 标签格式

php - 如何获取Wordpress简码中的字段值?

wordpress - 在 wordpress 短代码中使用双引号

php - 在 Wordpress 中嵌套短代码导致内部短代码根本不打印出来

php - WordPress:根据发布日期在之前发布的内容之后(或之前)添加文本

php - 通过 URL 添加到购物车的 WooCommerce 自定义购物车商品价格

css - 图片在 IE 中加载,但在 Firefox 或 Chrome 中不加载

php - 在 Woocommerce 单变量产品的变体选择器下添加短代码

php - 如何在非wordpress页面中调用简码?

wordpress - Woocommerce 过滤器自定义(四舍五入与下限)