Take the 2-minute tour ×







Those are the sites I've been researching the most trying to wrap my head around json

This is my php

if (have_posts()) :  
    while (have_posts()) : 
        $fvsc []= "\n" . '<div id="featuredv-' . "$wp_query->current_post\">" . "$post->post_content;" . '</div>';
$jsonObject= json_encode($fvsc);   

and this is my .js

    jQuery.getJSON("<?php TEMPLATEPATH . '/featuredv.php'?>",function(data){
        var fvsJA=jQuery.parseJSON(<?php $jsonObject; ?>);
        var fts = $("#ttContent>[id^=featuredt]");  
        var fvs = $("#inCenter>[id^=featuredv]");
    fts.click( function(){   
        $("#inCenter>[id^=featuredv]").replaceWith( function(){

What I'm trying to do is make my $fvsc array into a jQuery array. $fvsc is an array of videos, when a fts is clicked it should take the contents of a div and replace it with another video that has the same index as whichever fts was clicked. If you just load all the fts and fvs into one place you can use the click function here and it works great but if you echo out 5 videos and then later 10 and so on the site loads worse and worse. I thought if I could convert my php array into a jquery object that loads json strings then on click converts back into an object and replaces the div contents the load would degrade a lot better.

share|improve this question
Why aren't you echo()-ing the values from PHP into your JavaScript? –  salathe Oct 30 '11 at 0:04
I'm not quite sure what you mean, I think. If you try to set a php variable equal to echo or print the page won't load, so I don't think that's what you meant. I don't want to echo the contents out because they're videos, about 10 of them. I was trying to figure out how to pass the contents without having to echo them to the page. If that's not what you meant please explain what you're thinking because I would love any help really. I'm still pretty new so if it looks like I'm missing something obvious just let me know, I'm willing to study and apply just about anything. –  jrpelt Oct 30 '11 at 0:15
<?php $jsonObject; ?> will not write anything into your JavaScript, in other words it will look like jQuery.parseJSON(); –  salathe Oct 30 '11 at 0:29
Snap! You're totally right I didn't see that. But I'm 95% sure that my jQuery.getJSON() is failing so I wouldn't have noticed it for a while. –  jrpelt Oct 30 '11 at 0:37
Yes of course it is failing, because you don't write the URL into your JavaScript! –  salathe Oct 30 '11 at 11:23

1 Answer 1

I see several issues in your code:


    I don't think you can wrap class's variable in double quote. It should be something like this:

    $fvsc []= '<div id="featuredv-' . $wp_query->current_post . '">' . $post->post_content . '</div>';
  2. There's issue in your JS, specifically, the way you access URL. You see, ajax in wordpress is a bit different than the regular php. Look here for more information: http://wordpress.stackexchange.com/questions/9231/whats-the-preferred-method-of-writing-ajax-enabled-plugins/9236#9236

share|improve this answer
I got to you're answer about 8 mins after you answered but I wanted to read you're link first. I think it's got me pointed the direction I want, so I'm going to spend some time studying it. –  jrpelt Oct 30 '11 at 0:19

Your Answer


