Wordpress:在帖子中循环一个客户变量


Wordpress: Looping a customer variable within a post

我正在运行一个wordpress网站,我想知道是否可以在单个模板中对自定义字段进行循环?

以下是我在帖子中渲染youtube视频时使用的代码。我在帖子上记录的只是youtube的唯一标识符。

 <?php if ( get_post_meta($post->ID, 'video_youtube', true) ) : ?>
      <div class="youtube_video">
           <?php echo'
                <iframe width="570" height="395" src="http://www.youtube.com/embed/';?><?php echo get_post_meta($post->ID,'video_youtube', true)?><?php echo'" frameborder=0 allowfullscreen></iframe>
           ';
           ?>
      </div>
 <?php endif; ?>

我想要的是,每当在帖子中使用自定义变量"video_youtube"时,这段代码都会循环。

有人能提出什么建议吗?

我真的不明白,我得到的是,你想要某种方式或某种变量,当调用时生成youtube iframe代码。

但我不明白是你自己决定在模板上使用自定义变量的次数。您确定要自定义变量吗?或者你想要短代码?可以通过帖子/页面或模板填写。

试试这个

$youtube_embed = '
            <iframe width="570" height="395" src="http://www.youtube.com/embed/'.get_post_meta($post->ID, 'video_id', true).'" frameborder=0 allowfullscreen></iframe>
       ';
$youtube = get_post_meta($post->ID, 'video_youtube', true)?$youtube_embed;

现在,无论你想在哪里发布youtube视频,你都可以回声$youtube

或者,如果每次需要视频时都需要传递视频ID,则可以在模板文件中创建一个小FUNCTION甚至一个CLASS。

function embedYoutube($video_link,$is_embed){
return '<iframe width="570" height="395" 
src="http://www.youtube.com/embed/'.$video_link.'" frameborder=0 allowfullscreen></iframe>
       ';}

echo embedYoutube(get_post_meta($post->ID, 'video_id', true),get_post_meta($post->ID, 'video_youtube', true));