Kashif Waheed
Kashif Waheed

Reputation: 597

wordpress custom field values through jquery

i am working on a jquery slider that have thumbnails in the bottom and i want them to link with related videos. what i am doing for that is i am adding a post from admin, the title contain the title of image, the content area contain the thumbnail image and for the video link i am adding a custom field.

come to slider again if i am doing the same process for images only(not video) it work fine. i click on thumbnail in the bottom and large image displays in slider. i am getting the source of image that i am clicking through this line of code

   var url = $(this).attr("src");

it gives me the source of the image from img tag. every thing is fine. but now my exact point is that i want to get the custom field value through the above piece of code but i dont know how to do that as i tried so many methods randomly from the internet but nothing work fine for me.

hope you guys will understand what i am saying if not i will give further details with code if required.

any help will be grately appriciated.

here is my full code(php and 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>

here is the 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>  

Upvotes: 3

Views: 3868

Answers (2)

Tomas Ramirez Sarduy
Tomas Ramirez Sarduy

Reputation: 17471

You can use custom attributes prefixed with data-, add this when you retrieve every thumbnail:

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

And you can manipulate this "data" using Jquery. For example, to retrieve data in your example use this:

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

take a look at the jQuery documentation for more info/examples.

Upvotes: 0

Matanya
Matanya

Reputation: 6346

you need to use the get_post_meta() function to retrieve the desired custom field, like so:

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

Upvotes: 2

Related Questions