php - 在 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>'));
}
怎么了?
解决方案
这是工作代码
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 }}';
}
推荐阅读
- python - 为 Flask 创建身份验证装饰器的问题
- c++ - 为什么我得到可变参数模板函数的“调用 .. 没有匹配的函数”?
- sql - 选择其中的位置并具有相同的列
- laravel - 当数据库相同时,三个不同的laravel项目具有一种身份验证
- python - 如何使用 Python 从同一个文本文件中分离不同的输入格式
- rest - Bullhorn API 按添加日期查询作业订单
- c# - Xamarin AutoSuggestBox 文本未从代码隐藏设置
- php - 如何允许 CRON 访问目录或文件?
- python - Why is my recurrent neural network giving me an error?
- python - .NET 的 Debugger.Launch() 是否有等效的 Python 版本?