WordPress获取Visual Composer的Post Grid的发布数据

Jur*_*ter 4 php wordpress blogs

我在WordPress中使用Visual Composer,我想制作一个自定义的Post Grid。但是,后网格提供的默认元素还不够。我想显示该帖子的作者,其评论数,其类别以及其标签。我对Visual Composer并不是很熟悉,但是我需要正确的方向才能获取此数据?我能做什么?我已经搜索了他们的文件,但是没有运气。如果我需要四处移动php代码,我想知道我四处移动是正确的事情。有任何想法吗?如果您需要更多信息,请询问:D

在此先感谢您的帮助。

小智 5

我遇到了同样的问题;这是我的解决方法:

根据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);

}
Run Code Online (Sandbox Code Playgroud)

有一个小问题。它可以工作,但是get_the_author在WordPress中已弃用。我将不胜感激任何建议使其更现代,或者如果您提出其他替代方案,请提出建议。

另外,这是vc_post_id_render来自docs 的可用变量列表。他们来了:

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 (
  ),
))
Run Code Online (Sandbox Code Playgroud)


小智 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 }}';
}
Run Code Online (Sandbox Code Playgroud)


小智 1

这是你的回应

https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/

at 模板变量的使用

例如,在可视化作曲家模板中,您可以使用 {{ post_date:ID }} 来显示帖子 ID。我不知道如何显示标签。