【问题标题】:How to create this long shortcode by using a foreach loop (PHP + Wordpress)如何使用 foreach 循环(PHP + Wordpress)创建这个长短代码
【发布时间】:2017-08-02 21:50:41
【问题描述】:

谁能帮助我使用 foreach 循环创建一个短代码,如下所示?

这个短代码的目的是生成一个播放列表,并使用开始和结束标签:

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

在这些标签之间,每首要添加到播放列表的歌曲都有自己的短代码,如下所示:

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

假设用户的播放列表中有 2 首歌曲,整个短代码如下所示:

[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]

如您所见,每首歌曲的短代码一个接一个,包含在开始/结束标签中。


我认为可以做到这一点的最简单方法是使用 foreach 循环来生成每首歌曲的短代码。这是我写的简单函数。

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

这就是我得到的结果。

显然,这样做是错误的。更不用说,结束标签没有被占用,而是以文本形式显示。也许这就是为什么整个事情都不起作用的原因?

我应该提到,通过使用整个简码它自己可以正常工作。只有当我尝试以这种方式构建短代码时它才不起作用。

你会建议我如何完成这样的事情?非常感谢。


更新: 感谢大家的意见。这是我想出的,与@DACrosby 发布的和@TheManiac 建议的类似。

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]';
}

而我实际上需要生成的短代码,我正在使用:

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

它运行良好。再次感谢社区。​​p>

【问题讨论】:

  • 我可能没有关注,但如果您只是想生成简码文本,我认为您不应该使用 do_shortcode。将整个代码生成为单个字符串,然后在结果上调用do_shortcode
  • @TheManiac 这很有意义。让我试试看。你有机会给我一个快速的sn-p吗?不管怎样,谢谢!
  • 看起来 DACrosby 已经为你做了一个 sn-p。很确定他是对的。
  • @TheManiac 是的,我在你的建议后想出了什么。非常感谢。

标签: php wordpress loops foreach shortcode


【解决方案1】:

它可能不起作用,因为[/zoomsounds] 不是短代码 - 它是一个的结束标记。因此,分别运行第一个 [zoomsounds ...] 和关闭 [/zoomsounds] 不能按预期工作。就像评论中提到的 TheManiac 一样,先尝试构建字符串,然后只使用一个 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 );
}

【讨论】:

  • 在看到您的评论之前,我实际上已经解决了这个问题,但这几乎就是我所做的。我将在我的帖子中发布我的代码。非常感谢!
【解决方案2】:

只需将示例代码与一个简单的 for 循环一起打印出结果即可。

似乎是某些字符串值中包含特殊字符(例如:/'"")

您可以检查字符串值或使用下面的一些示例代码来测试该方法。

echo do_shortcode('[zoomsounds id="favorites_playlist"]');
        for($i = 0; $i < 5; $i++){
            $title = "FAV ".$i;
            $post = "FAV ".$i;
            $post_slug = "FAV ".$i;
            $source = "FAV ".$i;
            $song_name = "FAV ".$i;

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

然后打印出来: results

希望这会有所帮助。

【讨论】:

  • 有趣,让我看看这个,我会告诉你进展如何。还有,你是怎么打印出来的?谢谢!
  • 我使用了一个简单的自定义插件并将这个方法添加到我的自定义插件中,并在我的自定义模板上打印出来。
猜你喜欢
  • 2012-10-29
  • 1970-01-01
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
相关资源
最近更新 更多