Wordpress 在短代码中使用get_
我编写了一个轻量级插件,以生成自定义分类法中所有术语的列表,这非常有效。我想修改它,为一个特定的帖子生成一个所有术语的列表,但我正在努力找到正确的方法来获取这些术语Wordpress 在短代码中使用get_,wordpress,taxonomy,custom-taxonomy,taxonomy-terms,Wordpress,Taxonomy,Custom Taxonomy,Taxonomy Terms,我编写了一个轻量级插件,以生成自定义分类法中所有术语的列表,这非常有效。我想修改它,为一个特定的帖子生成一个所有术语的列表,但我正在努力找到正确的方法来获取这些术语 function antls_taxonomy_list( $atts ) { $atts = shortcode_atts( array( 'post_id' => '', 'name' => 'category', 'hide_empty' => fals
function antls_taxonomy_list( $atts ) {
$atts = shortcode_atts( array(
'post_id' => '',
'name' => 'category',
'hide_empty' => false,
'exclude' => '',
'include' => '',
'order' => 'ASC',
), $atts);
$terms = get_the_terms( array(
'post_id' => '',
'taxonomy' => $atts['name'],
'hide_empty' => $atts['hide_empty'],
'orderby' => 'name',
'order' => $atts['order'],
'parent' => 0
) );
完整代码如下(建议使用mods),它目前正在生成一条“此网站出现严重错误”消息
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define('ANTLS_PLUGIN_DIR_URL',plugin_dir_url( __FILE__ ) );
add_shortcode( 'tax_taxonomy_list', 'antls_taxonomy_list' );
function antls_taxonomy_list( $atts ) {
$atts = shortcode_atts( array(
'post_id' => '',
'name' => 'category',
'hide_empty' => false,
'exclude' => '',
'include' => '',
'order' => 'ASC',
), $atts);
global $post;
$terms = get_the_terms( array(
'post_id' => $post->ID, //ID from the $post object
'taxonomy' => $atts['name'],
'hide_empty' => $atts['hide_empty'],
'orderby' => 'name',
'order' => $atts['order'],
'parent' => 0
) );
$html .= '<ul class="antls tax-list-'.$atts['name'].'">';
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
if( !empty( $atts['exclude'] ) ) {
$exclude = explode( ',',$atts['exclude'] );
if( in_array( $term->term_id, $exclude) ) {
continue;
}
}
if( !empty( $atts['include'] ) ) {
$include = explode( ',',$atts['include'] );
if( !in_array( $term->term_id, $include) ) {
continue;
}
}
$html .= '<li class="tax-item-'.$atts['name'].'" data-taxname="'.strtolower( $term->name ).'">';
$term_link = get_term_link( $term );
$html .= '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
$html .= '</li>';
}
}
$html .= '</ul>';
return $html;
}
if(!defined('ABSPATH')){
出口
}
定义('ANTLS_PLUGIN_DIR_URL',PLUGIN_DIR_URL(__文件__));
添加快捷代码(“税务分类列表”、“蚂蚁分类列表”);
函数antls\u分类法\u列表($atts){
$atts=短码_atts(数组)(
“post_id'=>”,
“名称”=>“类别”,
“hide_empty”=>false,
'排除'=>'',
'包括'=>'',
“订单”=>“ASC”,
)(港币),;
全球$员额;
$terms=获取\u术语(数组)(
'post_id'=>$post->id,//来自$post对象的id
“分类法”=>$atts[“名称”],
'hide_empty'=>$atts['hide_empty'],
'orderby'=>'name',
“订单”=>$atts[“订单”],
“父项”=>0
) );
$html.='”;
如果(!empty($terms)&&!is_wp_error($terms)){
foreach($terms作为$term){
如果(!empty($atts['exclude'])){
$exclude=分解(“,”,$atts['exclude']);
if(在数组中($term->term\u id,$exclude)){
继续;
}
}
如果(!空($atts['include'])){
$include=explode(“,”,$atts['include']);
if(!in_数组($term->term_id,$include)){
继续;
}
}
$html.='- ”;
$term\u link=get\u term\u link($term);
$html.='';
$html.='
';
}
}
$html.='
';
返回$html;
}
在短代码中传递id属性将完成此操作
//Static
[shortcode id="123"]
//Dynamic. Get the ID of the current post
global $post; //Declare global post variable
$terms = get_the_terms( array(
'post_id' => $post->ID, //ID from the $post object
'taxonomy' => $atts['name'],
'hide_empty' => $atts['hide_empty'],
'orderby' => 'name',
'order' => $atts['order'],
'parent' => 0
) );
您需要将post ID传递给get_the_terms()函数。不幸的是,它导致了与我经历的相同的严重错误。我把完整的代码放在一把小提琴里。也许问题不在我想的地方?你犯了什么错误?我不在,回家后会回复。这条消息是:“这个网站出现了严重错误。”。我也应该这么做,我已经盯着这个太久了。您使用的短码是什么(包括您所有的ATT)[tax\u taxonomy\u list name=“category”hide\u empty=“true”exclude=“8”order=“ASC”]