Add category tag in masonry grid

This topic is: Not resolved

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

This topic contains 2 replies, has 2 voices, and was last updated by  Max 1 year, 2 months ago.

  • Author
    Posts
  • #11817

    denisslef
    Member
    Post count: 12

    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', 'yeahthemes' ),
         'base' => 'vc_post_cat',
         'category' => __( 'Post', 'yeahthemes' ),
         'description' => __( 'Show category label', 'yeahthemes' ),
         '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', 'yeahthemes' ), $category->name ) ) . '">'.$category->cat_name.'</a>';
      $cat_tag  .= '</span>';
      //$class    .= ' category-' . $category->slug;
    
      return $cat_tag;
    }
    • This topic was modified 1 year, 2 months ago by  denisslef.
    • This topic was modified 1 year, 2 months ago by  denisslef.
  • #11837

    denisslef
    Member
    Post count: 12

    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', 'yeahthemes' ), $category->name ) ) . '">'.$category->cat_name.'</a>';
        $cat_tag  .= '</span>';
        update_post_meta ( $post_id, 'cat-tag', $cat_tag );
      }
      
    }
    • This reply was modified 1 year, 2 months ago by  denisslef.
    • This reply was modified 1 year, 2 months ago by  denisslef.
    • This reply was modified 1 year, 2 months ago by  denisslef.
    • This reply was modified 1 year, 2 months ago by  denisslef.
  • #11861

    Max
    Staff
    Post count: 3150

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

    Login | Register | Forgot Password?

You must be logged in to reply to this topic.

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