1

我正在为WPBakery创建自定义元素。

我有一个名为的文件夹vc-elements,其中包含两个文件:

  1. hero.php
  2. text-image.php

在 WordPress 管理员方面,我希望这两个元素都是可见的。为此,functions.php我正在运行:

add_action( 'vc_before_init', 'vc_before_init_actions' );

function vc_before_init_actions() {

    // Link to VC elements's folder
    if( function_exists('vc_set_shortcodes_templates_dir') ){ 
        vc_set_shortcodes_templates_dir( get_template_directory() . 'vc-elements' );
    }
}

但是在管理方面,两个块都没有显示?

以前我有:

function vc_before_init_actions() {
    require_once( get_template_directory().'/vc-elements/hero.php' );  
}

这显示了管理员中的英雄块。但是当我添加时:

function vc_before_init_actions() {
    require_once( get_template_directory().'/vc-elements/hero.php' );
    require_once( get_template_directory().'/vc-elements/text-image.php' ); 
}

在管理方面,英雄元素被文本图像元素替换 - 一次只显示一个。这是为什么?

4

1 回答 1

1

您是使用父主题还是子主题构建它?我不确定你的标记。

通常,当我添加自定义模块时,我需要检查插件是否存在..

<?php
/**
  * Adds new shortcode "myprefix_say_hello" and registers it to
  * the Visual Composer plugin
  *
*/
   if ( ! class_exists( 'EZ_VC_Product_Widget' ) ) {
               class EZ_VC_Product_Widget {
    /**
     * Main constructor
    */
    public function __construct() {
        // Registers the shortcode in WordPress
        add_shortcode( 'ez_product', array( 'EZ_VC_Product_Widget', 'output' ) );

        // Map shortcode to Visual Composer
        if ( function_exists( 'vc_lean_map' ) ) {
            vc_lean_map( 'ez_product', array( 'EZ_VC_Product_Widget', 
         'map' ) );
        }
    }

    /**
     * Shortcode output
     */
    public static function output( $atts, $content = null ) {
        // Extract shortcode attributes (based on the vc_lean_map function - see next function)
        extract( vc_map_get_attributes( 'ez_product', $atts ) );

        // Define output
        $product = wc_get_product($id);

        if ( $id && $product) {
            global $post;
            $post = $product->get_post_data();

            setup_postdata( $post );

             ob_start();
                
               get_template_part('template-parts/product-module');

            wp_reset_postdata();
            return ob_get_clean();
        }
        else
            return '';
    }

    /**
    * Map shortcode to VC
    *
    * This is an array of all your settings which become the shortcode attributes ($atts)
    * for the output. See the link below for a description of all available parameters.
    *
    * @since 1.0.0
    * @link  https://kb.wpbakery.com/docs/inner-api/vc_map/
    */

    public static function map() {
        return array(
            'name'        => esc_html__( 'EZ Product', 'shopkeeper' ),
            'description' => esc_html__( 'Shortcode outputs a single product.', 'shopkeeper' ),
            'base'        => 'ez_product',
            "icon"        => get_stylesheet_directory_uri() . "/vc_extend/ez_shortcode_icon.png",
            'params'      => array(
                array(
                    'type' => 'autocomplete',
                    'heading' => esc_html__( 'Select identificator', 'js_composer' ),
                    'param_name' => 'id',
                    'description' => esc_html__( 'Input product ID or product SKU or product title to see suggestions', 'js_composer' ),
                ),
                array(
                    'type' => 'hidden',
                    // This will not show on render, but will be used when defining value for autocomplete
                    'param_name' => 'sku',
                ),
            ),
        );
    }
  }
}

  add_action('vc_before_init', function(){
 new EZ_VC_Product_Widget;
});


/**
  * @action wp_ajax_vc_get_autocomplete_suggestion - since 4.4 used to 
hook ajax requests for autocomplete suggestions
 */
 add_action( 'wp_ajax_vc_get_autocomplete_suggestion', 
'ez_vc_get_autocomplete_suggestion' );
   /**
   * @since 4.4
   */
  function ez_vc_get_autocomplete_suggestion() {
   vc_user_access()->checkAdminNonce()->validateDie()->wpAny( 
'edit_posts', 'edit_pages' )->validateDie();

$query = vc_post_param( 'query' );
$shortcode = wp_strip_all_tags( vc_post_param( 'shortcode' ) );


   if ( $shortcode == 'ez_product') {
    $tag = 'product';//wp_strip_all_tags( vc_post_param( 'shortcode' 
    ) 
  );
       $param_name = vc_post_param( 'param' );
        vc_render_suggestion( $query, $tag, $param_name );
       exit;
   }
}
于 2021-05-17T20:54:16.877 回答