Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Getting post data for Post Grid of Visual Composer

I'm using Visual Composer in WordPress and I want to make a custom Post Grid. But the default elements that the post grid supplies are not enough. I want to show the author of the post, the number of comments it has, the category it has and the Tags it has as well. I'm not really familiar with Visual Composer, but I need a point in the right direction for me to get this data? What can I do? I've search their documents but with no luck. If I need to move around the php code I would like to know what I'm moving around is the right thing. Any ideas? If you need any more information please do ask :D

Thanks in advance for the help.

like image 229
Jurgen Feuchter Avatar asked Jul 02 '15 23:07

Jurgen Feuchter


3 Answers

If anybody is still looking to find out how to to get the id in a post grid or create a specific widget for the grid you can use the following snippet I creatd for adding an icon before the post title. You will see inside the first function you can call $post-ID for use on any query.

//** 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 }}';
}

like image 56
Damian McCann Avatar answered Sep 18 '22 18:09

Damian McCann


I got the same issue; here is how I solve it:

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

When you add the code below to functions.php, a new component will be added in your custom grid builder (in my example the name will be "Author"). There are a number of values you can get by post data template variable function and one of it not the name of the author but their ID (that's sad but at least you can use this value to get the author name). The value is 'post_author' => ID of the author (for example '1')

Here is the function where I get the post author and display it (if author component was added to your custom grid in "custom grid builder"). Put it in functions.php of your child theme:

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

}

There is a little problem. It works but get_the_author is a deprecated function in WordPress. I'd appreciate any suggestions to make it more modern or if you name other alternatives please suggest.

Also, here is the list of available variables of vc_post_id_render from docs. Here they are:

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 (
  ),
))
like image 32
Aleksey Spirin Avatar answered Sep 19 '22 18:09

Aleksey Spirin


this is your response

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

at Template variables usage

Exemple, in visual composer template you can use {{ post_date:ID }} to show post ID. I don't know how show tag.

like image 23
Hipopeur Avatar answered Sep 20 '22 18:09

Hipopeur