我遇到了同样的问题;这是我的解决方法:
根据 Visual Composer 文档:https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/
当您将以下代码添加到functions.php 时,将在您的自定义网格构建器中添加一个新组件(在我的示例中,名称将是“作者”)。您可以通过发布数据模板变量函数获得许多值,其中一个不是作者的姓名,而是他们的 ID(这很可悲,但至少您可以使用此值来获取作者姓名)。
值为作者的'post_author' => ID(例如'1')
这是我获取帖子作者并显示它的功能(如果作者组件已添加到“自定义网格构建器”中的自定义网格中)。把它放在你孩子主题的functions.php:
add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_post_id'] = array(
'name' => __( 'Author', 'my-text-domain' ),
'base' => 'vc_post_id',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Show current post author', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
// output function
add_shortcode( 'vc_post_id', 'vc_post_id_render' );
function vc_post_id_render() {
$nn = '{{ post_data:post_author }}'; // usage of template variable post_data with argument "post_author"
return get_the_author($nn);
}
有个小问题。它可以工作,但 get_the_author 是 WordPress 中已弃用的功能。我很感激任何使它更现代的建议,或者如果您提出其他替代方案,请提出建议。
此外,这里是来自文档的vc_post_id_render 的可用变量列表。他们在这里:
WP_Post::__set_state(array(
'ID' => 69,
'post_author' => '1',
'post_date' => '2015-04-29 14:15:56',
'post_date_gmt' => '2015-04-29 14:15:56',
'post_content' => 'Your post content',
'post_title' => 'Your post title',
'post_excerpt' => '',
'post_status' => 'publish',
'comment_status' => 'open',
'ping_status' => 'open',
'post_password' => '',
'post_name' => 'post name',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2015-06-17 11:18:41',
'post_modified_gmt' => '2015-06-17 11:18:41',
'post_content_filtered' => '',
'post_parent' => 0,
'guid' => 'http://wp.master/?p=69',
'menu_order' => 0,
'post_type' => 'post',
'post_mime_type' => '',
'comment_count' => '0',
'filter' => 'raw',
'filter_terms' =>
array (
),
))