我正在开发一个jquery滑块,在底部有缩略图,我想让它们与相关视频链接。我所做的是,我从管理员添加一个帖子,标题包含图像的标题,内容区域包含缩略图图像和视频链接,我添加了一个自定义字段。
再次来滑块,如果我只是为图像(不是视频)做同样的过程,它工作得很好。我点击底部的缩略图,并在滑块中显示大图像。我正在通过这行代码获得我正在单击的图像的源代码
var url = $(this).attr("src");它为我提供了来自img标签的图像的来源。一切都很好。但是现在我要说的是,我想通过上面的代码获得自定义字段的值,但我不知道该怎么做,因为我从互联网上随机尝试了这么多方法,但没有一种方法对我很有效。
希望你们会理解我的话,如果没有,我会给出更多的细节代码,如果需要的话。
任何帮助都将被给予适当的评价。
这是我的完整代码(php和html)
</div>
<div class="video-gallery">
<a class="prev browse left"><<</a>
<div class="scrollable" id="scrollable">
<div class="items">
<?php if (have_posts()) : ?>
<?php
$thePosts = get_posts('numberposts=50&category=4');
foreach($thePosts as $post) :
setup_postdata($post);
$custom_field = get_post_meta($post->ID,'video path', true);
echo '<div class="myclass" style="display:none" id="'.$custom_field.'"></div>';
the_content();?>
<?php endforeach; ?>
<?php else : ?>
No Results
<?php endif; ?>
</div>
</div>下面是javascript jquery
<script type="text/javascript">
$(".video-gallery img").click(function() {
// see if same thumb is being clicked
if ($(this).hasClass("active")) { return; }
// calclulate large image's URL based on the thumbnail URL (flickr specific)
var url = $('.myclass').attr('id');
alert(url);
// get handle to element that wraps the image and make it semi-transparent
var wrap = $(".image_wrap").fadeTo("medium", 0.5);
// the large image from www.flickr.com
var img = new Image();
// call this function after it's loaded
img.onload = function() {
// make wrapper fully visible
wrap.fadeTo("fast", 1);
// change the image
wrap.find("iframe").attr("src", url);
};
// begin loading the image from www.flickr.com
img.src = url;
// activate item
$(".video-gallery img").removeClass("active");
$(this).addClass("active");
// when page loads simulate a "click" on the first image
});
</script> 发布于 2012-11-20 15:51:38
您需要使用get_post_meta()函数来检索所需的自定义字段,如下所示:
$custom_field = get_post_meta($post->ID,'CustomFieldName', true);发布于 2012-11-20 16:19:33
您可以使用前缀为data-的自定义属性,在检索每个缩略图时添加以下内容:
<img data-video="get_post_meta($post->ID,'video_ulr', true);" src="...">您可以使用Jquery操作这些“数据”。例如,要检索示例中的数据,请使用以下代码:
var url = $('this').data('video');有关更多信息/示例,请查看jQuery documentation。
https://stackoverflow.com/questions/13468597
复制相似问题