1

I created a shortcode for visual composer to get terms of a custom taxonomy. But when I get them (for a category dropdown on visual composer's backend editor) it's not working.

enter image description here My code:

$args1= array(
   ‘taxonomy’ => ‘danh_muc’, **// my custom taxonomy**
   ‘hide_empty’ => 0,
);
$inra=array();
$danhmucs= get_terms($args1);
foreach ($danhmucs as $danhmuc) {
   $tendm=$danhmuc-> name;
   $iddm=$danhmuc-> term_id;
   $inra[$tendm]=$iddm;
}
vc_map( array(
   “name” => __(“News”, ‘understrap’),
   “base” => “tintuc”,
   “class” => “”,
   “category” => ‘Content’,
   “icon” => “icon-wpb-application-icon-large”,
   “params” => array(
      array(
         “type” => “dropdown”,
         “holder” => “div”,
         “class” => “”,
         “heading” => “Danh mục”,
         “param_name” => “cat”,
         “value” => $inra, //**array taxonomy by name=> id**
         “description” => ‘Chọn danh mục BĐS cần hiện bài đăng’,
      ),
   )
));

But when I change ‘taxonomy’ => ‘danh_muc’ to ‘taxonomy’ => ‘category’ it's working fine!

I var_dump $inra with 2 cases: ‘taxonomy’ => ‘danh_muc’ and ‘taxonomy’ => ‘category’, The results are the same.

Thanks all!

0

Are there actually any terms in your dahn_muc custom taxonomy? get_terms won't return anything if no terms have been created.

Also, how did you create your custom taxonomy? I usually rely on GenerateWP.com's taxonomy generator to make sure everything's done properly.

Lastly, it might be better to reverse how you populate your $inra array. Use the term IDs as the keys, instead of the values:

foreach ( $danhmucs as $danhmuc ) {
    ...
    $inra[$iddm] = $tendm;
    ...
}
2
  • Thanks sir, I create taxonomy by CPT UI Plugin. When I var_dump $inra the result array ('name taxonomy'=>'ID',...), it fine!
    – aminor1993
    Mar 27 '17 at 6:45
  • Can you try creating your custom post types with generatewp.com's generator, instead of the CPT UI plugin? I'm not exactly sure what's happening, but it's possible that the post type created by CPT UI isn't "seen" by Visual Composer.
    – ricotheque
    Mar 29 '17 at 3:40
0

This post is quite old but it helped me to create the code I needed. As I have a working solution, thanks to @ricotheque and @aminor1993 I wanted to post my code here. Maybe it helps someone looking for the same.

I have a custom post type called 'reference' which have categories as taxonomies called 'reference_category'.

I want the user to be able to select a category, created on the reference custom post type, so I can show him the reference items which are assigned to the selected category.

Here is my code:

$references=array();
$reference_items = get_terms (
    array(
        "taxonomy" => "reference_category",
        "hide_empty" => 0,
    )
);
foreach ($reference_items as $item) {
    $itemName = $item-> name;
    $itemId = $item-> term_id;
    $references[$itemId] = $itemName;
}


vc_map(
    array(
        "name" => __("Reference"),
        "base" => "ci_vc_reference",
        "icon" => "ci-vc-icon",
        "category" => __('Colibri Interactive', 'ciframework-vc'),
        "params" => array(
            array(
                "type" => "dropdown",
                "holder" => "div",
                "class" => "",
                "heading" => __('Show references of category', 'ciframework-vc'),
                "param_name" => "category",
                "value" => $references,
            ),
       )
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.