如何在WP Bakery中返回vc_link?

huangapple go评论103阅读模式
英文:

how to return vc_link in wp bakery

问题

我正在尝试在wpbakery构建器中创建自定义元素,其中我正在添加文本字段、图像和URL。一切都正常工作,只是在URL上有一个问题。有人可以帮我解决这个问题吗?

以下是我正在尝试的代码:

add_shortcode('expert', 'expert_func');
function expert_func($atts, $content = null) { // 添加了新的函数参数$content!
  extract(
    shortcode_atts(
      array(
        'expert_image' => 'expert_image',
        'title' => '',
      ),
      $atts
    )
  );

  $img_url = wp_get_attachment_image_src($expert_image, "full");

  extract(
    shortcode_atts(
      array(
        'expert_exp_image' => 'expert_exp_image',
        'title' => '',
      ),
      $atts
    )
  );

  $exp_img_url = wp_get_attachment_image_src($expert_exp_image, "full");

  $result .= '<div class="inner__card">
                <div class="expert-img">
                  <img src="' . $img_url[0] . '">
                </div>
                <div class="expert-details">
                  <p class="name">' . $atts['expert_name'] . '</p>
                  <p class="designation">' . $atts['expert_designation'] . '</p>
                  <div class="expect-experience">
                    <div class="expect-experience-no">
                      <img src="' . $exp_img_url[0] . '" alt="experience-10">
                    </div>
                    <div class="expect-experience-text">
                      <p>' . $atts['expert_short_desc'] . '</span></p>
                    </div>
                  </div>
                  <p class="details">' . $atts['expert_description'] . '</p>
                  <div class="expert-btn">';

  if (!empty($url)) {
    $column_link_array = vc_build_link($url);
    $url = $column_link_array['url'];
    $result .= "<a href='$url' class='learn-btn'></a>";
  }
  $result .= ' </div>
                </div>
            </div>';

  //$result = $button;

  //print_r($atts);

  return $result;
}

//后端
add_action('vc_before_init', 'expert_integrateWithVC');
function expert_integrateWithVC() {
  vc_map(array(
    "name" => __("Expert", "my-text-domain"),
    "base" => "expert",
    "class" => "",
    "category" => __("Custom", "my-text-domain"),
    "params" => array(
      array(
        "type" => "attach_image",
        "holder" => "div",
        "class" => "",
        "heading" => __("Choose Expert Image", "my-text-domain"),
        "param_name" => "expert_image",
        "value" => __(""),
      ),
      array(
        "type" => "textfield",
        "holder" => "div",
        "class" => "",
        "heading" => __("Expert Name", "my-text-domain"),
        "param_name" => "expert_name",
        "value" => "",
      ),
      array(
        "type" => "textfield",
        "holder" => "div",
        "class" => "",
        "heading" => __("Expert Designation", "my-text-domain"),
        "param_name" => "expert_designation",
        "value" => "",
      ),
      array(
        "type" => "attach_image",
        "holder" => "div",
        "class" => "",
        "heading" => __("Add Year of Expirience image", "my-text-domain"),
        "param_name" => "expert_exp_image",
        "value" => "",
      ),
      array(
        "type" => "textfield",
        "holder" => "div",
        "class" => "",
        "heading" => __("Expert's Expirience Short Description", "my-text-domain"),
        "param_name" => "expert_short_desc",
        "value" => "",
      ),
      array(
        "type" => "textarea",
        "holder" => "div",
        "class" => "",
        "heading" => __("Short Description of Expert", "my-text-domain"),
        "param_name" => "expert_description",
        "value" => "",
      ),
      array(
        "type" => "vc_link",
        "holder" => "div",
        "class" => "",
        "heading" => __("Choose Redirect URL", "my-text-domain"),
        "param_name" => "url",
        "value" => "",
        "description" => __("Add Short Description", "my-text-domain"),
      )
    )
  ));
}

我只想要返回所选的URL。我是WordPress的新手。谢谢您的帮助。

英文:

I am trying to create custom element in wpbakery builder in which i am adding textfield, image and url. Evrything is working fine just there is one issue in url . can anybody please help me out to solve this issue.

Below is the code i am trying :

add_shortcode( &#39;expert&#39;, &#39;expert_func&#39; );
function expert_func( $atts, $content = null ) { // New function parameter $content is added!
extract(
shortcode_atts(
array(
&#39;expert_image&#39; =&gt; &#39;expert_image&#39;,
&#39;title&#39;   =&gt; &#39;&#39;,
),
$atts
)
);
$img_url = wp_get_attachment_image_src( $expert_image, &quot;full&quot;);
extract(
shortcode_atts(
array(
&#39;expert_exp_image&#39; =&gt; &#39;expert_exp_image&#39;,
&#39;title&#39;   =&gt; &#39;&#39;,
),
$atts
)
);
$exp_img_url = wp_get_attachment_image_src( $expert_exp_image, &quot;full&quot;);
$result .=&#39;&lt;div class=&quot;inner__card&quot;&gt;
&lt;div class=&quot;expert-img&quot;&gt;
&lt;img src=&quot;&#39;. $img_url[0] .&#39;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;expert-details&quot;&gt;
&lt;p class=&quot;name&quot;&gt;&#39;.$atts[&#39;expert_name&#39;].&#39;&lt;/p&gt;
&lt;p class=&quot;designation&quot;&gt;&#39;.$atts[&#39;expert_designation&#39;].&#39;&lt;/p&gt;
&lt;div class=&quot;expect-experience&quot;&gt;
&lt;div class=&quot;expect-experience-no&quot;&gt;
&lt;img src=&quot;&#39;. $exp_img_url[0] .&#39;&quot; alt=&quot;experience-10&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;expect-experience-text&quot;&gt;
&lt;p&gt;&#39;.$atts[&#39;expert_short_desc&#39;].&#39;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p class=&quot;details&quot;&gt;&#39;.$atts[&#39;expert_description&#39;].&#39;&lt;/p&gt;
&lt;div class=&quot;expert-btn&quot;&gt;&#39;;
if( !empty($url) ){
$column_link_array = vc_build_link($url);
$url = $column_link_array[&#39;url&#39;];
$result .= &quot;&lt;a href=&#39;$url&#39; class=&#39;learn-btn&#39;&gt;&lt;/a&gt;&quot;;
}
$result .=&#39; &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&#39;;
//$result = $button;
//print_r($atts);
return $result;
}
//backend
add_action( &#39;vc_before_init&#39;, &#39;expert_integrateWithVC&#39; );
function expert_integrateWithVC() {
vc_map( array(
&quot;name&quot; =&gt; __( &quot;Expert&quot;, &quot;my-text-domain&quot; ),
&quot;base&quot; =&gt; &quot;expert&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;category&quot; =&gt; __( &quot;Custom&quot;, &quot;my-text-domain&quot;),
&quot;params&quot; =&gt; array(
array(
&quot;type&quot; =&gt; &quot;attach_image&quot;,
&quot;holder&quot; =&gt; &quot;div&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;heading&quot; =&gt; __( &quot;Choose Expert Image&quot;, &quot;my-text-domain&quot; ),
&quot;param_name&quot; =&gt; &quot;expert_image&quot;,
&quot;value&quot; =&gt; __( &quot;&quot;, &quot;my-text-domain&quot; )
),
array(
&quot;type&quot; =&gt; &quot;textfield&quot;,
&quot;holder&quot; =&gt; &quot;div&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;heading&quot; =&gt; __( &quot;Expert Name&quot;, &quot;my-text-domain&quot; ),
&quot;param_name&quot; =&gt; &quot;expert_name&quot;,
&quot;value&quot; =&gt; __( &quot;&quot;, &quot;my-text-domain&quot; )
),
array(
&quot;type&quot; =&gt; &quot;textfield&quot;,
&quot;holder&quot; =&gt; &quot;div&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;heading&quot; =&gt; __( &quot;Expert Designation&quot;, &quot;my-text-domain&quot; ),
&quot;param_name&quot; =&gt; &quot;expert_designation&quot;,
&quot;value&quot; =&gt; __( &quot;&quot;, &quot;my-text-domain&quot; )
),
array(
&quot;type&quot; =&gt; &quot;attach_image&quot;,
&quot;holder&quot; =&gt; &quot;div&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;heading&quot; =&gt; __( &quot;Add Year of Expirience image&quot;, &quot;my-text-domain&quot; ),
&quot;param_name&quot; =&gt; &quot;expert_exp_image&quot;,
&quot;value&quot; =&gt; __( &quot;&quot;, &quot;my-text-domain&quot; )
),
array(
&quot;type&quot; =&gt; &quot;textfield&quot;,
&quot;holder&quot; =&gt; &quot;div&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;heading&quot; =&gt; __( &quot;Expert&#39;s Expirience Short Description&quot;, &quot;my-text-domain&quot; ),
&quot;param_name&quot; =&gt; &quot;expert_short_desc&quot;,
&quot;value&quot; =&gt; __( &quot;&quot;, &quot;my-text-domain&quot; )
),
array(
&quot;type&quot; =&gt; &quot;textarea&quot;,
&quot;holder&quot; =&gt; &quot;div&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;heading&quot; =&gt; __( &quot;Short Description of Expert&quot;, &quot;my-text-domain&quot; ),
&quot;param_name&quot; =&gt; &quot;expert_description&quot;,
&quot;value&quot; =&gt; __( &quot;&quot;, &quot;my-text-domain&quot; )
),
array(
&quot;type&quot; =&gt; &quot;vc_link&quot;,
&quot;holder&quot; =&gt; &quot;div&quot;,
&quot;class&quot; =&gt; &quot;&quot;,
&quot;heading&quot; =&gt; __( &quot;Choose Rediect URL&quot;, &quot;my-text-domain&quot; ),
&quot;param_name&quot; =&gt; &quot;url&quot;,
&quot;value&quot; =&gt; __( &quot;&quot;, &quot;my-text-domain&quot; ),
&quot;description&quot; =&gt; __( &quot;Add Short Description&quot;, &quot;my-text-domain&quot; )
)
)
) );
}

All i want is to return URL selected. i am new in wordpress.
Thank you in advance.

答案1

得分: 3

你没有提取你的URL,所以 if( !empty($url) ) { 不会触发,你应该将所有的变量都添加到你的提取代码中。

function expert_func( $atts ) {

  extract( shortcode_atts( array(
    'expert_image' => '',
    'expert_name' => '',
    'expert_designation' => '',
    'expert_exp_image' => '',
    'expert_short_desc' => '',
    'expert_description' => '',
    'url' => '',
  ), $atts ) );

  $img_url = wp_get_attachment_image_src( $expert_image, "full");

  $url_link = vc_build_link($url);

  // 你的代码...

  if (isset($url_link['url'])) {

    $result .= '<a href="'.esc_url($url_link['url']).'" class="learn-btn">'.$url_link['title'].'</a>';

  }

  // 你的其他代码...

}
英文:

You are not extracting your URL so if( !empty($url) ) { wont fire, you should add all your variables to your extract code.

function expert_func( $atts ) {
extract( shortcode_atts( array(
&#39;expert_image&#39;  =&gt; &#39;&#39;,
&#39;expert_name&#39;  =&gt; &#39;&#39;,
&#39;expert_designation&#39; =&gt; &#39;&#39;,
&#39;expert_exp_image&#39; =&gt; &#39;&#39;,
&#39;expert_short_desc&#39; =&gt; &#39;&#39;,
&#39;expert_description&#39;  =&gt; &#39;&#39;,
&#39;url&#39; =&gt; &#39;&#39;,
), $atts ) );
$img_url = wp_get_attachment_image_src( $expert_image, &quot;full&quot;);
$url_link = vc_build_link($url);
your code...
if (isset($url_link[&#39;url&#39;])) {
$result .= &#39;&lt;a href=&quot;&#39;.esc_url($url_link[&#39;url&#39;]).&#39;&quot; class=&quot;learn-btn&quot;&gt;&#39;.$url_link[&#39;title&#39;].&#39;&lt;/a&gt;&#39;;
}
your code etc...
}

答案2

得分: 0

可以检查(附有截图)
$use_link = true;

如何在WP Bakery中返回vc_link?

英文:

Can check ( Screenshot attached )

$use_link = true;

如何在WP Bakery中返回vc_link?

huangapple
  • 本文由 发表于 2020年1月3日 18:09:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定