我使用 WPBakery 的 API 开发了一个插件,它基本上允许用户选择帖子类型,以及要在前端显示的分类。我想要实现的是动态显示附加字段的能力,这取决于首先选择哪个父分类。
例如,我在一个多下拉字段中显示所有父分类。根据选择的分类法,然后我想显示其他下拉列表,其中这些分类法的子级作为选择。
在这一点上,我觉得我唯一的解决方案是输出大量动态命名的字段(就像在每个父分类的每个子术语中一样),并使用一些 JavaScript 来显示/隐藏那些被选为下拉列表的字段。这似乎是一个糟糕的实现,DOM 中有过多的 HTML(尽管是隐藏的)。
我也考虑过将参数注入 DOM,但我不完全确定如何在后端处理它们,因为它们可能没有注册。
有一个param_group
,但它似乎用于转发器字段,而不是动态人口。
我的代码如下所示:
function YYY() {
add_shortcode('YYY', 'YYY');
$markup = 'YYY';
$types = get_post_types([], 'array');
$post_types = array("All");
foreach($types as $type) {
$post_types[] = $type - > name;
}
$taxonomies = get_taxonomies();
foreach($taxonomies as $taxonomy) {
$post_taxonomies[] = $taxonomy;
}
vc_map(array(
"name" => __("YYY"),
"base" => "YYY",
"category" => __('Content'),
"custom_markup" => $markup, // @TODO how do we access shortcode's attributes here to display in bakery grid
"params" => array(
array(
'type' => 'dropdown_multi',
'heading' => __('Select a post type', VAE_TEXT_DOMAIN),
'param_name' => 'post_types',
'value' => $post_types,
'description' => __('Select a post type', VAE_TEXT_DOMAIN),
'admin_label' => false,
'save_always' => true,
),
array(
"type" => "dropdown_multi",
"holder" => "div",
"class" => "",
"heading" => __("Count"),
"param_name" => "count",
"value" => array("All", "3", "6"),
"description" => __("How many tiles to show")
),
array(
"type" => "dropdown_multi",
"holder" => "div",
"class" => "",
"heading" => __("Select Taxonomy(ies)"),
"param_name" => "taxonomies",
"value" => $taxonomies,
"description" => __("Post with selected taxonomies will be shown")
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "",
"heading" => __("Taxonomies Operator"),
"param_name" => "taxonomies_operator",
"value" => array("IN - posts which match ANY selected taxonomy", "NOT IN - post which DO NOT match ANY selected taxonomy", "AND - posts which match ALL selected taxonomy"),
"description" => __("Posts with selected Tags will be shown")
), //@TODO how do I dynamically add dropdowns here with key/value pairs dynamically populated by above taxonomies dropdown
array(
"type" => "dropdown",
"holder" => "div",
"class" => "",
"heading" => __("Header Style"),
"param_name" => "header_style",
"value" => array("h2", "h3", "Visual Style/Color"),
"description" => __("")
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "",
"heading" => __("Read More Text"),
"param_name" => "read_more_text",
"value" => array("Read More", "Learn More", "Download"),
"description" => __("")
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "",
"heading" => __("Link Target"),
"param_name" => "link_target",
"value" => array("Self", "Blank"),
"description" => __("")
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "",
"heading" => __("Order By"),
"param_name" => "order_by",
"value" => array("Date"),
"description" => __("")
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "",
"heading" => __("Sort By"),
"param_name" => "sort_by",
"value" => array("ASC", "DESC"),
"description" => __("")
),
array(
"type" => "checkbox",
"holder" => "div",
"class" => "",
"heading" => __("Front-End Filter (Only shows chosen taxonomies)"),
"param_name" => "front_end_filter",
"value" => array("Show" => "show", "Hide" => "hide"),
"description" => __("")
),
array(
"type" => "checkbox",
"holder" => "div",
"class" => "",
"heading" => __("Pagination"),
"param_name" => "pagination",
"value" => array("Show" => "show", "Hide" => "hide"),
"description" => __("")
),
array(
"type" => "checkbox",
"holder" => "div",
"class" => "",
"heading" => __("Icon"),
"param_name" => "icon",
"value" => array("Show" => "show", "Hide" => "hide"),
"description" => __("")
),
array(
"type" => "checkbox",
"holder" => "div",
"class" => "",
"heading" => __("Featured Image"),
"param_name" => "featured_image",
"value" => array("Show" => "show", "Hide" => "hide"),
"description" => __("")
),
array(
"type" => "checkbox",
"holder" => "div",
"class" => "",
"heading" => __("Excerpt"),
"param_name" => "excerpt",
"value" => array("Show" => "show", "Hide" => "hide"),
"description" => __("")
),
array(
"type" => "textfield",
"holder" => "div",
"class" => "",
"heading" => __("Row Class"),
"param_name" => "row_class",
"value" => "",
"description" => __("")
),
array(
"type" => "textfield",
"holder" => "div",
"class" => "",
"heading" => __("Row ID"),
"param_name" => "row_id",
"value" => "",
"description" => __("")
),
)
));
}