wordpress - 如何访问 vc_map() 内部值来为 WPBakery 界面创建自定义标记?
问题描述
此处提出了类似的问题,但没有任何可行的答复:Visual Composer custom markup for custom shortcode (vc_map)
在这里:Visual Composer 自定义简码模板 - custom_markup 显示用户输入
我正在尝试在 WPBakery 界面中创建自定义标记。
我可以通过以下方式添加自定义标记:
$markup = 'test';
vc_map( array(
"name" => __("MyShortcode"),
"base" => "myshortcode",
"category" => __('Content'),
"custom_markup" => $markup, // @TODO how do we access shortcode's attributes here to display in bakery grid
"params" => $params
) );
这将在 WPBakery 网格中输出“测试”,这很好,但是如何访问 vc_map() 内部值来显示?
例如,我将“帖子类型”作为此短代码的字段。例如,如果有人选择“页面”帖子类型,我想在 WPBakery 网格中显示这些帖子。我无法弄清楚的是如何获取用户选择显示的值。
任何帮助将不胜感激。我一直在无休止地寻找这个。
解决方案
我找到了解决方案。通过扩展 WPBakeryShortCode 类,您可以更改 contentAdmin 输出:
// Extend Class WPBakeryShortCodesContainer
if (class_exists('WPBakeryShortCode')) {
class WPBakeryShortCode_Custom_Element extends WPBakeryShortCode {
/**
* @param $atts
* @param $content
*
* @return string
* @throws \Exception
*/
public function contentAdmin( $atts, $content = null ) {
$output = $custom_markup = $width = $el_position = '';
if ( null !== $content ) {
$content = wpautop( stripslashes( $content ) );
}
$shortcode_attributes = array( 'width' => '1/1' );
$atts = vc_map_get_attributes( $this->getShortcode(), $atts ) + $shortcode_attributes;
$this->atts = $atts;
$elem = $this->getElementHolder( $width );
if ( isset( $this->settings['custom_markup'] ) && '' !== $this->settings['custom_markup'] ) {
$markup = $this->settings['custom_markup'];
$elem = str_ireplace( '%wpb_element_content%', $this->customMarkup( $markup, $content ), $elem );
$output .= $elem;
} else {
$inner = $this->outputTitle( $this->settings['name'] );
$inner .= $this->paramsHtmlHolders( $atts );
$elem = str_ireplace( '%wpb_element_content%', $inner, $elem );
$output .= $elem;
}
return $output;
}
}
}
我按照这个 Git 示例创建我的自定义元素:https ://gist.github.com/Webcreations907/ff0b068c5c364f63e45d
推荐阅读
- c++ - 运行单元测试,不包括 Catch2 中的特定标签
- python - Jupyter notebook 输出指针
- google-cloud-dataflow - 工作人员启动后数据流作业未运行?
- javascript - 什么是正确的运算符 < 或 &&
- python - 在 SIGSEGV 之后处理芹菜中的 WorkerLostError
- coldfusion - ColdFusion 在基于脚本的 cfc 函数中使用 if 条件
- javascript - 使用引导模式时有没有办法记录页面视图
- angular - 如何使用 POST 方法在 Angular 中发送表单数据?
- mysql - MySQL 查询在 linux shell 脚本中中断,但查询在 mysql 命令行中运行
- java - Flink中的时间窗口加入不一致?