我使用嵌套元素 wp bakery 。我正在尝试使用类和公共函数。
但无法正常工作。它没有注册。我认为这可能是一个问题,因为我使用了 class 。请找出问题所在。我不需要变通。我知道如何使用成员函数来处理它。但我需要使用成员函数来完成
<?php
/*
* Element Description: Featured Block
*/
//featured block container
if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
class protechsaasFeaturedBlock extends WPBakeryShortCodesContainer {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'protechsaas_featured_block_mapping' ) );
add_shortcode( 'feature_container', array( $this, 'protechsaas_featured_block_html' ) );
}
// Element Mapping
public function protechsaas_featured_block_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
//Register "container" content element. It will hold all your inner (child) content elements
vc_map( array(
"name" => __("Feature Block", "protechsaas"),
"base" => "feature_container",
"as_parent" => array('only' => 'feature'), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
"content_element" => true,
"show_settings_on_create" => false,
"is_container" => true,
"params" => array(
// add params same as with any other content element
array(
"type" => "textfield",
"heading" => __("Block Title", "protechsaas"),
'holder' => 'h2',
'class' => 'sub-title-class',
"param_name" => "subtitle",
"description" => __("add the title for your features block", "protechsaas")
),
array(
"type" => "textfield",
"heading" => __("Title", "protechsaas"),
'holder' => 'h2',
'class' => 'title-class',
"param_name" => "title",
"description" => __("add the main title for your features block", "protechsaas")
),
array(
"type" => "textarea",
"heading" => __("Content", "protechsaas"),
'holder' => 'p',
'class' => 'content-class',
"param_name" => "content",
"description" => __("add the main content for your features block", "protechsaas")
),
array(
'type' => 'dropdown',
'holder' => 'div',
'class' => 'style-class',
'heading' => __( 'Block Style', 'protechsaas' ),
'param_name' => 'blockstyle',
'value' => array(
'1' => 'Feature white bg ',
'2' => 'Feature grey bg',
'3' => 'Feature icon bg none ',
),
'description' => __( 'choose you features block style', 'protechsaas' ),
'admin_label' => true,
'weight' => 0,
),
),
"js_view" => 'VcColumnView'
) );
}
// Element HTML
public function protechsaas_featured_block_html( $atts ,$features = null ) {
// Params extraction
extract(
shortcode_atts(
array(
'subtitle' => '',
'title' => '',
'content' => '',
'blockstyle' => '',
),
$atts
)
);
switch ($blockstyle) {
case '1':
$html = '
<section class="client-speak our-features padding-lg">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing">
'.do_shortcode($features).'
</ul>
</div>
</section>';
break;
case '2':
$html = '
<section class="client-speak our-features padding-lg bg-white">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing ico-bg">
'.do_shortcode($features).'
</ul>
</div>
</section>';
break;
case '3':
$html = '
<section class="client-speak our-features padding-lg">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing bg-none">
'.do_shortcode($features).'
</ul>
</div>
</section> ';
break;
default:
$html = '
<section class="client-speak our-features padding-lg">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing">
'.do_shortcode($features).'
</ul>
</div>
</section>';
break;
}
return $html;
}
}
}
//feature element
if ( class_exists( 'WPBakeryShortCode' ) ) {
class protechsaasFeature extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'protechsaas_feature_mapping' ) );
add_shortcode( 'protechsaas_feature', array( $this, 'protechsaas_feature_html' ) );
}
// Element Mapping
public function protechsaas_feature_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
vc_map( array(
"name" => __("Feature", "protechsaas"),
"base" => "feature",
"content_element" => true,
"as_child" => array('only' => 'feature_container'), // Use only|except attributes to limit parent (separate multiple values with comma)
"params" => array(
// add params same as with any other content element
array(
"type" => "textfield",
"heading" => __("title", "protechsaas"),
"param_name" => "title",
"description" => __("add the title for your feature.", "protechsaas")
),
array(
"type" => "textarea",
"heading" => __("Content", "protechsaas"),
'holder' => 'p',
'class' => 'content-class',
"param_name" => "content",
"description" => __("add the content for your feature", "protechsaas")
),
array(
'type' => 'attach_image',
'holder'=> 'div',
'class' => '',
'heading' => __('Icon image', 'protechsaas'),
'param_name' => 'iconimage',
'description' => __('Choose an image for icon if ypu want use your own icons else keep it blank and choose icon from next field', 'protechsaas'),
),
array(
'type' => 'dropdown',
'holder' => 'div',
'class' => 'style-class',
'heading' => __( 'Saas Icons', 'protechsaas' ),
'param_name' => 'icon',
'value' => array(
'icon-analytics' => 'analytics ',
'icon-responsive' => 'responsive',
'icon-support' => 'support',
'icon-settings' => 'settings',
'icon-file' => 'file',
'icon-graphic' => 'graphic',
),
'description' => __( 'choose you features block style', 'protechsaas' ),
'admin_label' => true,
'weight' => 0,
),
)
) );
}
// Element HTML
public function protechsaas_feature_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'iconimage' => '',
'title' => '',
'content' => '',
'icon' => '',
),
$atts
)
);
if($iconimage != null){
$bg=wp_get_attachment_image_src($iconimage,'full');
$iconcontent = '<img src="'.$bg[0].'" alt="icon" class="img-fluid"/>';
}
else {
$iconcontent ='<span class="'.$icon.'"></span>';
}
$html='
<li class="col-md-4">
<div class="inner"> <span class="icon">'.$iconcontent.'</span>
<h3>'.$title.'</h3>
<p>'.$content.'</p>
</div>
</li>
';
return $html;
}
}
}
// Element Class Init
new protechsaasFeaturedBlock();
new protechsaasFeature();
?>
我遇到了同样的问题,花了一些时间来解决这个问题。我们不想脱离我们的命名空间环境,并保持我们的类名一致。
您可以将一个额外的未记录的属性传递给您的 vc_map 函数,那就是
php_class_name
。
所以你的代码是:
vc_map(array(
'name' => __('Feature', 'protechsaas'),
'php_class_name' => 'protechsaasFeaturedBlock',
));
if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
class protechsaasFeaturedBlock extends WPBakeryShortCodesContainer {
// Your code here
}
}
这也适用于命名空间:
'php_class_name' => '\Company\Package\protechsaasFeaturedBlock'
据我所知,这没有记录,但如果您要扩展
WPBakeryShortCodesContainer
类,您的类名必须以 WPBakeryShortCode_
为前缀,并且后缀必须与您正在使用的短代码库的名称相匹配。可能还有其他限制,我还没有广泛测试它。
所以如果在
vc_map()
你正在使用:
"base" => "feature_container",
然后将您的班级声明更改为:
class WPBakeryShortCode_feature_container extends WPBakeryShortCodesContainer
我知道这没有意义,但经过大量测试后,这是我的代码失败的原因。我以为这是因为我也将其声明为一个类,而不是直接调用
vc_map()
.
你甚至不必声明一个短代码就可以工作,所以我猜如果没有什么东西的话,
WPBakeryShortCodesContainer
类中的某些东西实际上会从 base
参数中注册短代码?
是的,如果你想保持你的命名空间干净/一致,这个 mega 很糟糕 ¯\_(ツ)_/¯
我复制了这个作为嵌套元素的例子。我发现您的代码中有错字。这一行:
add_shortcode( 'protechsaas_feature', array( $this, 'protechsaas_feature_html' ) );
功能名称是
feature
而不是protechsaas_feature
。这是在这里定义的:
"base" => "feature",
所以你的代码应该变成:
add_shortcode( 'feature', array( $this, 'protechsaas_feature_html' ) );
我在这里尝试了所有答案但没有成功(使用 WPBakery 6.9)。
vc_map 参数“php_class_name”什么都不做......不知道为什么有些人让它工作。 关于自定义类名的未记录规则是领先一步,它确实需要“WPBakeryShortCode_”+“base”,但这还不够。
所以我设法解决了一些问题。同样覆盖核心类的一个方法,我的答案在这里:https://stackoverflow.com/a/75721948/9039682