我在WordPress中使用Visual Composer,我想制作一个自定义的Post Grid。但是帖子网格提供的默认元素是不够的。我想展示帖子的作者,评论的数量,它的类别以及它的标签。我对Visual Composer并不熟悉,但我需要一个正确的方向来获取这些数据?我能做什么?我搜索他们的文件但没有运气。如果我需要移动PHP代码,我想知道我移动的是正确的。有任何想法吗?如果您需要更多信息,请询问:D
提前感谢您的帮助。
答案 0 :(得分:5)
我遇到了同样的问题;这是我如何解决它:
根据Visual Composer文档:https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/
当您将以下代码添加到if (dtpCheckIn >= new DateTime(2016, 1, 1) && dtpCheckIn < new DateTime(2016, 5, 1))
{
}
时,您的自定义网格构建器中将添加一个新组件(在我的示例中,名称将为&#34;作者&#34;)。你可以通过发布数据模板变量函数获得许多值,其中一个不是作者的名字,而是他们的ID(这很难过,但至少你可以使用这个值来获取作者姓名)。
值为作者的functions.php
(例如&#39; 1&#39;)
以下是我获得帖子作者并显示它的功能(如果作者组件已添加到&#34;自定义网格构建器&#34;中的自定义网格中)。把它放在你孩子主题的'post_author' => ID
中:
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);
}
是WordPress中不推荐使用的功能。我很欣赏任何使其更现代化的建议,或者如果您提出其他替代方案,请提出建议。
此外,以下是来自docs的get_the_author
的可用变量列表。他们在这里:
vc_post_id_render
答案 1 :(得分:5)
如果有人仍在寻找如何在帖子网格中获取ID或为网格创建特定小部件,您可以使用以下代码片段创建我在帖子标题之前添加图标。您将在第一个函数中看到,您可以调用$post-ID
以用于任何查询。
//** Case Study Title Block Shortcodes ***********
//********************************
add_filter( 'vc_gitem_template_attribute_case_study_title','vc_gitem_template_attribute_case_study_title', 10, 2 );
function vc_gitem_template_attribute_case_study_title( $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
$title = get_the_title($post->ID);
$link = get_permalink($post->ID);
$terms = get_the_terms($post->ID, 'case_categories');
$output = "<h4 class=\"case-title\"><a href=\"". $link ."\">". get_the_icon($terms[0]->term_id) . $title ."</a></h4>";
return $output;
}
add_filter( 'vc_grid_item_shortcodes', 'case_study_title_shortcodes' );
function case_study_title_shortcodes( $shortcodes ) {
$shortcodes['vc_case_study_title'] = array(
'name' => __( 'Case Study Title', 'sage' ),
'base' => 'vc_case_study_title',
'icon' => get_template_directory_uri() . '/assets/images/icon.svg',
'category' => __( 'Content', 'sage' ),
'description' => __( 'Displays the case study title with correct icon', 'sage' ),
'post_type' => Vc_Grid_Item_Editor::postType()
);
return $shortcodes;
}
add_shortcode( 'vc_case_study_title', 'vc_case_study_title_render' );
function vc_case_study_title_render($atts){
$atts = vc_map_get_attributes( 'vc_case_study_title', $atts );
return '{{ case_study_title }}';
}
答案 2 :(得分:1)
这是你的回复
https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/
在模板变量用法
例如,在可视化作曲家模板中,您可以使用{{post_date:ID}}来显示帖子ID。我不知道如何展示标签。