0

我已经使用具有简单标题、WISYWIG 内容和背景图像的 vc_map() 制定了一个自定义 Visual Composer 元素。我的问题是,我在内容块内使用的任何简码都未被检测到,并且要么作为原始简码吐出,要么,如果我通过 do_shortcode 调用,则在保存时被注释掉。

// Element Class
class vcInfoBox extends WPBakeryShortCode {

// Element Init
function __construct() {
  //  add_action( 'init', array( $this, 'vc_infobox_mapping' ) );
  $this->vc_infobox_mapping();
}

// Element Mapping
public function vc_infobox_mapping() {

    // Stop all if VC is not enabled
    if ( !defined( 'WPB_VC_VERSION' ) ) {
        return;
    }

    // Map the block with vc_map()
    vc_map(
        array(
            'name' => __('VC Infobox', 'text-domain'),
            'base' => 'vc_infobox',
            'description' => __('A simple callout box with backing image', 'text-domain'),
            'category' => __('Custom Elements', 'text-domain'),
            'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
            'params' => array(

                array(
                    'type' => 'textfield',
                    'holder' => 'h2',
                    'class' => 'title-class',
                    'heading' => __( 'Title', 'text-domain' ),
                    'param_name' => 'title',
                    // 'value' => __( 'Default value', 'text-domain' ),
                    'description' => __( 'Box Title', 'text-domain' ),
                    'admin_label' => false,
                    'weight' => 0,
                    'group' => 'Custom Group',
                ),

                array(
                    'type' => 'textarea_html',
                    'holder' => 'div',
                    // 'class' => 'text-class',
                    'heading' => __( 'Content', 'text-domain' ),
                    'param_name' => 'content',
                    'value' => __( '', 'text-domain' ),
                    'description' => __( 'Main content inside block', 'text-domain' ),
                    // 'admin_label' => false,
                    // 'weight' => 0,
                    'group' => 'Custom Group',
                ),

                array(
                    'type' => 'attach_image',
                    'holder' => 'img',
                    //'class' => 'text-class',
                    'heading' => __( 'Background Image', 'text-domain' ),
                    'param_name' => 'bgimg',
                    // 'value' => __( 'Default value', 'text-domain' ),
                    'description' => __( 'Image to be displayed behind callout block', 'text-domain' ),
                    'admin_label' => false,
                    'weight' => 0,
                    'group' => 'Custom Group',
                ),

                array(
                    'type' => 'dropdown',
                    'class' => '',
                    'heading' => __( 'Alignment', 'text-domain' ),
                    'param_name' => 'align',
                    'value' => array(
                      'Left' => "align-left",
                      'Right' => "align-right",
                    ),
                    'description' => __( 'Left or right alignment for callout block?', 'text-domain' ),
                    'admin_label' => false,
                    'weight' => 0,
                    'group' => 'Custom Group',
                ),

            ),
        )
    );

}


// Element HTML
public function vc_infobox_html( $atts, $content ) {

    // Params extraction
    extract(
        shortcode_atts(
            array(
                'title' => '',
                'bgimg' => 'bgimg',
                'align' => '',
            ),
            $atts
        )
    );

    $img_url = wp_get_attachment_image_src( $bgimg, "large");
    // die ( print_r($align, true) );

    // Fill $html var with data
    $html = '
    <div class="info-callout '. $align .'" style="background-image:url('. $img_url[0] .');">
        <div class="info-callout--content-wrap">
          <h2 class="info-callout--title">'. $title .'</h2>
          <div class="info-callout--content">'. $content .'</div>
        </div>
    </div>';

    return $html;

}

} // End Element Class

在functions.php中:

require_once( get_stylesheet_directory().'/vc-elements/custom-callout-block.php' );

add_action( 'vc_before_init', 'vc_before_init_actions' );

function vc_before_init_actions() {

    $InfoBox = new vcInfoBox();
    add_shortcode( 'vc_infobox', array( $InfoBox, 'vc_infobox_html' ) );

}

我已经将我的数组与 VC 中的默认“文本块”元素进行了比较,它们似乎匹配......有人知道我错过了什么吗?

4

1 回答 1

0

这个问题似乎太老了。但这里是修复。更改此行

function __construct() {

add_action( 'init', array( $this, 'vc_infobox_mapping' ) );

function __construct() {
add_action( 'vc_before_init', array( $this, 'vc_infobox_mapping' ) );

还有functions.php上的那个

add_action( 'vc_before_init', 'vc_before_init_actions' );

add_action( 'init', 'vc_before_init_actions' );
于 2020-11-03T15:06:22.450 回答