【问题标题】:PHP variable containing iframe with dynamic src?PHP 变量包含带有动态 src 的 iframe?
【发布时间】:2018-12-29 23:21:07
【问题描述】:

我在我的网站上使用 ACF,并尝试设置一个 PHP 变量,它是一个包含 ACF 字段的 iframe。

我不知道为什么这不起作用,因为我可以回显变量并显示它,但它会吐出 src(unknown) 否则?

// Get spotify and set up tab
if (have_rows('roster_media')):
    // loop through the rows of data
    while (have_rows('roster_media')): the_row();
        $spotify_uri = get_sub_field('rm_sp_link');
        echo $spotify_uri;
        $url = $spotify_uri;
        echo $url;
    endwhile;
else:
// no rows found
endif;

$rm_sp = '<iframe src="'.$url.'" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>';

然后在我通过以下方式调用的页面中:

<?php

$count = 0;
foreach ($embed_links as $e) {

    if ($count==0) {
        $rm_act = " active show";
    } else {
        $rm_act = "";
    }

    $content;

    if ($e['embed_platform']['value'] === "rm_sp") {
        $content ='<div class="rm-video">'.$rm_sp.'</div>';   
    } else if ($e['embed_platform']['value']==="rm_vid") {
        $content = '<div class="rm-video">'.$rm_vid.'</div>';
    }

    echo '<div class="tab-pane'.$rm_act.'" id="'.$e['embed_platform']['value'].'" role="tabpanel">'.$content.'</div>';
    $count++;
}

?>

【问题讨论】:

  • 对于任何想知道的人,我目前正在使用 jQuery(我认为这不是最好的方法,但它对我有用)。

标签: php html iframe wordpress-theming advanced-custom-fields


【解决方案1】:

我确信这是最糟糕的方法,但它目前对我有用,因为我没有其他想法。我正在使用 jQuery 来实现这一点。

在while循环中,我添加了:

$spotify_uri = get_sub_field('rm_sp_link');
echo '<div id="spot_id">'.$spotify_uri.'</div>';

然后我的 jQuery 看起来像这样:

  if ($("#spot_id")[0]){
    // Do something if class exists
    spotifyUri = $("#spot_id").text();
    console.log(spotifyUri);
    $("#spot_iframe").attr('src' , $("#spot_iframe").attr('src') + spotifyUri);
  } else {
      // Do something if class does not exist
  } 

显然,#spot_id 也需要对用户隐藏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多