首页 > 解决方案 > 在 Grid Builder Visual Composer 中获取帖子标签

问题描述

我正在尝试在自定义网格构建器中输出帖子标签,它允许按键输出自定义元字段值,但不允许使用简单的 wp 标签。我尝试按照说明进行操作,这是我添加了短代码的代码,但仍然没有输出任何标签

add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
function my_module_add_grid_shortcodes( $shortcodes ) {
 $shortcodes['vc_mytags'] = array(
  'name' => __( 'My Tags', 'my-text-domain' ),
  'base' => 'vc_mytags',
  'category' => __( 'Post', 'my-text-domain' ),
  'description' => __( 'Show post tags', 'my-text-domain' ),
  'post_type' => Vc_Grid_Item_Editor::postType(),
 );

 return $shortcodes;
}
// output function
add_shortcode( 'vc_mytags', 'vc_mytags_render' );
function vc_mytags_render($atts, $content, $tag) {
 return '{{ mytags }}';
}

add_filter( 'vc_gitem_template_attribute_mytags', 'vc_gitem_template_attribute_mytags ', 10, 2 );
function vc_gitem_template_attribute_mytags( $value, $data ) {
 /**
  * @var Wp_Post $post
  * @var string $data
  */
 extract( array_merge( array(
  'post' => null,
  'data' => '',
 ), $data ) );

 return var_export( get_the_tag_list('<p>Tags: ',', ','</p>'));
}

怎么了?

标签: phpwordpressvisual-composer

解决方案


这是工作代码

add_filter('vc_gitem_template_attribute_mytags','vc_gitem_template_attribute_mytags', 10, 2 );
function vc_gitem_template_attribute_mytags( $value, $data ) {
    extract( array_merge( array(
      'post' => null,
      'data' => '',
   ), $data ) );
  $atts_extended = array();
  parse_str( $data, $atts_extended );
  $atts = $atts_extended['atts'];
  // write all your widget code in here using queries etc
$mytags = get_the_tag_list('<p>Tags: ',', ','</p>');
 return $mytags;
}

add_filter( 'vc_grid_item_shortcodes', 'mytags_shortcodes' );
function mytags_shortcodes( $shortcodes ) {
   $shortcodes['vc_mytags'] = array(
     'name' => __( 'Tags', 'sage' ),
     'base' => 'vc_mytags',
     'category' => __( 'Content', 'sage' ),
     'description' => __( 'Displays tags', 'sage' ),
     'post_type' => Vc_Grid_Item_Editor::postType()
  );
  return $shortcodes;
 }

add_shortcode( 'vc_mytags', 'vc_mytags_render' );
function vc_mytags_render($atts){
   $atts = vc_map_get_attributes( 'vc_mytags', $atts );
   return '{{ mytags }}';
}

推荐阅读