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;
    ...
}
  • 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

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.