2012-11-20 33 views
3

我正在jQuery的滑块,底部有缩略图,我希望他们与相关的视频链接。我正在为此做的是我从管理员添加一篇文章,标题包含图像标题,内容区域包含缩略图图像和视频链接我添加一个自定义字段。WordPress的自定义字段值通过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> 

回答

2

您需要使用get_post_meta()函数来检索所需的自定义字段,像这样:

$custom_field = get_post_meta($post->ID,'CustomFieldName', true); 
+0

是的,你是对的我已经知道我可以通过这个函数得到它,但我想通过jqery得到它而不是php –

+0

你正在混合客户端和服务器端。您无法使用JQuery从远程服务器检索数据。您必须使用服务器端语言,例如PHP。一旦你已经检索到值,你可以将它分配给一个JavaScript变量,并对其进行处理,如下所示:var custom_field = <?php echo $ custom_field; ?>; – Matanya

+0

是的,看起来最好让我试试这个 –

0

您可以使用数据 - 前缀自定义属性,当你检索每一个缩略图补充一点:

<img data-video="get_post_meta($post->ID,'video_ulr', true);" src="..."> 

你可以操纵这个“数据”使用jQuery。例如,若要检索您的数据。例如使用:

var url = $('this').data('video'); 

看看在jQuery documentation获取更多信息/例子。

+0

还未定义的问题 –

+0

@KashifWaheed:您正在使用不正确的引号:试试这个:'echo'

';' –

+0

@KashifWaheed:如果您仍然收到一个空字符串,请尝试使用var_dump($ custom_field)来获取值由函数(应该是“BOOL FALSE”如果没有这样的字段存在)返回 – Matanya

相关问题