drexlz0623 2017-08-02 21:50
浏览 29
已采纳

如何使用foreach循环创建这个长短代码(PHP + Wordpress)

Can anyone help me create a shortcode such as the one below by using a foreach loop?

The purpose of this shortcode is to generate a playlist, and uses opening and closing tags:

[zoomsounds id="favorites_playlist"] [/zoomsounds]

In between those tags, each song that is to be added to the playlist gets its own shortcode, like this:

[zoomsounds_player config="favorites-playlist" source="'.$source.'" type="detect" songname="'.$title.'" init_player="off" play_target="footer"]

So let's say a user has 2 songs in their playlist, the entire shortcode would look something like this:

[zoomsounds id="favorites_playlist"][zoomsounds_player config="favorites-playlist" source="http://www.test.com/sound/brobob.mp3" type="detect" songname="Song 1" init_player="off" play_target="footer"][zoomsounds_player config="favorites-playlist" source="http://www.test.com/sound/brobob2.mp3" type="detect" songname="Song 2" init_player="off" play_target="footer"][/zoomsounds]

As you can see, each song's shortcode comes one after another, enclosed in the start/close tags.


The easiest way I thought I could do this was to use a foreach loop to generate each song's shortcode. Here is the simple function I wrote.

function streamFavoritesPlaylist() {
    $favs[] = get_user_favorites();

    echo do_shortcode('[zoomsounds id="favorites_playlist"]');
        foreach ($favs[0] as $fav) {
            $title = get_the_title($fav);
            $post = get_post($fav);
            $post_slug = $post->post_name;
            $source = '.../mp3/'.$post_slug.'.mp3';
            $song_name = get_the_title($fav);

            echo do_shortcode('[zoomsounds_player config="favorites-playlist" source="'.$source.'" type="detect" songname="'.$title.'" init_player="off" play_target="footer"]');
        }
    echo do_shortcode('[/zoomsounds]');
}

And this is the result that I get. enter image description here

Clearly this is the wrong way to go about doing something like this. Not to mention, the closing tag did not take, and instead has been displayed in text. Perhaps this is why the entire thing is not working?

I should mention that by using the entire shortcode on it's own works fine. It's only when I try to build the shortcode in this manner does it not work.

How would you suggest I go about accomplishing something like this? Thanks a lot.


UPDATE: Thanks for everyone's input. This is what I came up with, which is similar to what @DACrosby posted, and @TheManiac suggested.

function streamFavoritesPlaylist() {
    $favs[] = get_user_favorites();
    $shortcode_full = "";

    foreach ($favs[0] as $fav) {
        $title = get_the_title($fav);
        $post = get_post($fav);
        $post_slug = $post->post_name;
        $source = '.../mp3/'.$post_slug.'.mp3';
        $song_name = get_the_title($fav);
        $shortcode = '[zoomsounds_player config="favorites-playlist" source="'.$source.'" type="detect" songname="'.$title.'" init_player="off" play_target="footer"]';

        $shortcode_full .= $shortcode;
    }

    return '[zoomsounds id="favorites_playlist"]'.$shortcode_full.'[/zoomsounds]';
}

And where I actually need the shortcode generated, I am using:

<?php echo do_shortcode(streamFavoritesPlaylist()); ?>

It's working perfectly. Thanks again, community.

  • 写回答

2条回答 默认 最新

  • duanli0687 2017-08-02 22:23
    关注

    It's probably not working because [/zoomsounds] isn't a shortcode - it's the closing tag of one. So running the first [zoomsounds ...] and the closing [/zoomsounds] separately doesn't work as expected. Like TheManiac mentioned in a comment, try building the string first then having just one do_shortcode

    function streamFavoritesPlaylist() {
        $favs[] = get_user_favorites();
        $str = '[zoomsounds id="favorites_playlist"]';
    
        foreach ($favs[0] as $fav) {
            $title = get_the_title($fav);
            $post = get_post($fav);
            $post_slug = $post->post_name;
            $source = '.../mp3/'.$post_slug.'.mp3';
            $song_name = get_the_title($fav);
    
            $str .= '[zoomsounds_player config="favorites-playlist"'
                  . ' source="'.$source.'" type="detect"'
                  . ' songname="'.$title.'" init_player="off"'
                  . ' play_target="footer"]';
        }
        $str .= '[/zoomsounds]';
        echo do_shortcode( $str );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面