【问题标题】:Return multiple results with same short code使用相同的短代码返回多个结果
【发布时间】:2021-04-01 21:50:52
【问题描述】:

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

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

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');

【问题讨论】:

    标签: php wordpress function shortcode


    【解决方案1】:

    首选的方式是将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"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 2016-08-02
      相关资源
      最近更新 更多