短代码不返回两个相同的属性

因此,由于某种原因,我无法提取相同的链接属性并将它们传递到我的代码中。


所以这是短代码:


[slide headline="<h2>Title</h2>" image="https://via.placeholder.com/150" body="The text in the body" link="Test1|https://www.test1.com" link="Test2|https://test2.com"]


var_dump($atts)仅返回一个链接,如下所示:


array (size=4)

  'headline' => string '</p>

<h2>Title</h2>

<p>' (length=29)

  'image' => string 'https://www.raritanheadwaters.org/wp-content/uploads/2018/01/placeholder-picture-large-opt.png' (length=94)

  'body' => string 'The text in the body' (length=202)

  'link' => string 'Test1|https://www.test1.com' (length=61)

这是我的代码:


function slide_item_shortcode($atts, $content = null)

{

    shortcode_atts([

            "image" => '',

            "headline" => '',

            "body" => '',

            "link" => '',

        ], $atts);


    var_dump($atts);


    $social_links = explode("|", $atts['link']);


    return '<li class="slide">

            <p><img src="' . esc_url($atts['image']) . '" alt="" /></p>

            <p>'. $atts['headline'] .'</p>

            <p>'. $atts['body'] .'</p>

            <p>'. '<a href="' . $social_links[1] . '" target="_blank">' . $social_links[0] . '</a>' .'</p>

            </li>';

}

add_shortcode('slide', 'slide_item_shortcode');


万千封印
浏览 101回答 2
2回答

沧海一幻觉

您可以通过对不同角色使用额外爆炸来传递多个链接[slide ... links="Test1|https://www.test1.com,Test2|https://www.test2.com"]function slide_item_shortcode($atts, $content = null){&nbsp; &nbsp; shortcode_atts([&nbsp; &nbsp; &nbsp; &nbsp; "image" => '',&nbsp; &nbsp; &nbsp; &nbsp; "headline" => '',&nbsp; &nbsp; &nbsp; &nbsp; "body" => '',&nbsp; &nbsp; &nbsp; &nbsp; "links" => '',&nbsp; &nbsp; ], $atts);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $linkPairs = explode(',', $atts['links']); // separate the pairs&nbsp; &nbsp; $output = '<li class="slide">';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; foreach($linkPairs as $linkPair) {&nbsp; &nbsp; &nbsp; &nbsp; $pair = explode('|', $linkPair); // separate the title and url&nbsp; &nbsp; &nbsp; &nbsp; $output .=&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<p><img src="' . esc_url($atts['image']) . '" alt="' . $pair[0] .'" /></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>'. $atts['headline'] .'</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>'. $atts['body'] .'</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>'. '<a href="' . $pair[1] . '" target="_blank">' . $pair[0] . '</a>' .'</p>';&nbsp; &nbsp; }&nbsp; &nbsp; $output .= '</li>';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $output;}

慕村9548890

我建议您不要在属性内使用 html,为此再创建一个属性来覆盖默认值,我还为示例的链接添加了另一个参数:短代码:[myshortcode heading="Text of heading" heading_tag="h3" links="Name of link 1|#url1|blank, Name of Link2|#url2"]和处理程序本身function linkParser($source) {&nbsp; $args = array_map('trim', explode('|',&nbsp; trim($source)));&nbsp; $keys = ['url_name', 'url', 'target'];&nbsp; $results = [];&nbsp; foreach($args as $key => $value) {&nbsp; &nbsp; $results[(isset($keys[$key]) ? $keys[$key] : $key)] = $value;&nbsp; }&nbsp; return $results;}function myShortCode($attrs, $content = null) {&nbsp; $attrs = shortcode_atts([&nbsp; &nbsp; 'heading_tag' => 'h2',&nbsp; &nbsp; "heading" => "",&nbsp; &nbsp; "links" => ""&nbsp; ], $attrs);&nbsp; $heading = sprintf('<%1$s>%2$s</%1$s>', (!empty($attrs['heading_tag']) ? $attrs['heading_tag'] : 'h2'), $attrs['heading']);&nbsp; $links = [];&nbsp; if(!empty($attrs['links'])) {&nbsp; &nbsp; $links = array_map('linkParser', explode(',', $attrs['links']));&nbsp; }&nbsp; // Tests with kint debug&nbsp; d($heading);&nbsp; d($links);}add_shortcode('myshortcode', 'myShortCode');
打开App,查看更多内容
随时随地看视频慕课网APP