首页 > 解决方案 > 如何访问 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 网格中显示这些帖子。我无法弄清楚的是如何获取用户选择显示的值。

任何帮助将不胜感激。我一直在无休止地寻找这个。

标签: wordpressvisual-composerwpbakery

解决方案


我找到了解决方案。通过扩展 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


推荐阅读