Add category tag in masonry grid

This is a PRIVATE forum for verified users only, to view all replies/answers, you must be logged in!

Status: not resolved
  • This topic has 2 replies, 2 voices, and was last updated 3 y, 6 mo by  Max.
Viewing 2 reply threads
  • Author
    Posts
    • #11817

      denisslef
      Member

      Hello

      I’m trying to add the coloured category tags in the grid elements, but I can’t find a way to do it. I’ve read the Visual Composer documentation but I can’t make it work

      Currently my code is

      add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
      function my_module_add_grid_shortcodes( $shortcodes ) {
         $shortcodes['vc_post_cat'] = array(
           'name' => __( 'Post category', 'wpthms' ),
           'base' => 'vc_post_cat',
           'category' => __( 'Post', 'wpthms' ),
           'description' => __( 'Show category label', 'wpthms' ),
           'post_type' => Vc_Grid_Item_Editor::postType(),
        );
       
         return $shortcodes;
      }
       
      // output function
      add_shortcode( 'vc_post_cat', 'vc_post_cat_render' );
      function vc_post_cat_render() {
        global $post;
        $categories = get_the_category($post->ID );
       
        $category   = $categories[0];
        $cat_tag    .= '<span class="entry-category">';
        $cat_tag  .= '<a class="cat-tag ' . esc_attr( $category->slug ) . '" href="'. esc_attr( get_category_link( $category->term_id ) ) .'" title="' . esc_attr( sprintf( __( 'View all posts in %s', 'wpthms' ), $category->name ) ) . '">'.$category->cat_name.'</a>';
        $cat_tag  .= '</span>';
        //$class    .= ' category-' . $category->slug;
      
        return $cat_tag;
      }
      • This topic was modified 3 y, 7 mo by  denisslef.
      • This topic was modified 3 y, 7 mo by  denisslef.
    • #11837

      denisslef
      Member

      Ok I finally found a solution, I post it here.

      Solution is to add meta to post according to their categories

      add_action( 'save_post', 'add_meta_cat_on_post_save' );
      
      function add_meta_cat_on_post_save($post_id){
        global $post, $current_user;
        if( !current_user_can( 'manage_options' ) ) return;
        $catID = $_POST['post_category'][1];
        $categories = get_category( $catID );
      
        if( !empty( $categories )) {
        $category = $categories;
          $cat_tag    = '<span class="entry-category">';
          $cat_tag  .= '<a>slug ) . '" href="'. esc_attr( get_category_link( $category->term_id ) ) .'" title="' . esc_attr( sprintf( __( 'View all posts in %s', 'wpthms' ), $category->name ) ) . '">'.$category->cat_name.'</a>';
          $cat_tag  .= '</span>';
          update_post_meta ( $post_id, 'cat-tag', $cat_tag );
        }
        
      }
      • This reply was modified 3 y, 7 mo by  denisslef.
      • This reply was modified 3 y, 7 mo by  denisslef.
      • This reply was modified 3 y, 7 mo by  denisslef.
      • This reply was modified 3 y, 7 mo by  denisslef.
    • #11861

      Max
      Staff

      Hey there, You must be logged in to view this reply!

      Login | Register | Forgot Password?

Viewing 2 reply threads
  • You must be logged in to reply to this topic.

This is a PRIVATE forum for verified users only, to view all replies/answers, you must be logged in!

© Copyright 2019 by wpthms. All Rights Reserved.