首页 > 解决方案 > 使用相同的短代码返回多个结果

问题描述

我在其中添加了一个函数functions.php,它适用于一个带有提到的链接的结果,$link但是我试图用这个函数显示来自类似链接的多个结果。尝试使用两个变量返回数组$atlantic1$atlantic2添加第二个链接$link2,还尝试使用新的短代码创建另一个函数以获得第二个结果,但没有任何效果。

解决此问题的最佳方法是什么?

function fish($bubbles) {
    extract(shortcode_atts(array(
        "fin" => get_option('pacific'),
    ), $bubbles));


    $width = " width=\"".$fin['width']."\"";
    $height = " height=\"".$fin['height']."\"";
    $osorientation = " orientationMode=\"manual\"";
    
        
    $link = "https://pacific.local/item/1/A";   
    $path = parse_url($link, PHP_URL_PATH);
    $segments = explode('/', rtrim($path, '/'));
    
    wp_enqueue_script( 'some-pacific-js' ); 

    $atlantic = "<fish-info fAdd=\"".$segments[2]."\" fId=\"".$segments[3]."\" network=\"".$network."\"></fish-info>";

    return $atlantic;
    
}
add_filter('widget_text', 'do_shortcode');
add_shortcode('pacific', 'fish');

标签: phpwordpressfunctionshortcode

解决方案


首选方式是link作为简码的参数传递,然后使用特定链接调用简码两次。

首先,改变鱼功能

function fish($atts) {
    // Pay attention, I removed "extract" so $fin is now a key inside $atts
    $atts = shortcode_atts( array(
      'fin' => get_option('pacific'),
      'item' => 'CHOOSE_A_DEFAULT'
    ), $atts );


    $width = " width=\"".$atts['fin']['width']."\"";
    $height = " height=\"".$atts['fin']['height']."\"";
    $osorientation = " orientationMode=\"manual\"";

    
    $link = "https://pacific.local/item/" . $atts['item'];   
    $path = parse_url($link, PHP_URL_PATH);
    $segments = explode('/', rtrim($path, '/'));

    wp_enqueue_script( 'some-pacific-js' ); 

    $atlantic = "<fish-info fAdd=\"".$segments[2]."\" fId=\"".$segments[3]."\" 
    network=\"".$network."\"></fish-info>";

    return $atlantic;

}

在您调用短代码的小部件中,您可能有如下内容:

[pacific]

你必须把它改成这样:

[pacific item="1/A"]
[pacific item="2/B"]

推荐阅读