0

Im using a cool plugin called "wp ajax random posts". It basically lets me call a function into a template that can auto refresh a loop of posts. It works great until I tried to change the loop structure inside the function. My posts have embedded youtube videos so I set it up so the section was only calling the video and its ratings. Originally it was just for a title and that worked fine but when I added the video it returns a second post under the original that was suppose to dissapear. From there it refreshes the bottom video like normal which boggles my mind. Here's the original function and then the modified one. Can someone point out why this is happening?

function WARP_Random_posts($args=''){
$defargs=array('number' => 8, 'cmtcount' => 0, 'excerpt' => 0, 'length' => 100, 'auto' => 0, 'time' => 60);
$args = wp_parse_args($args, $defargs);$output='';$number=$args['number'];
query_posts("showposts=$number&orderby=rand&cat=3");
if(have_posts()){
    while (have_posts()) :the_post();
        $output.='<li id="random-post-'.get_the_ID().'" class="random-post"><div class="random-post-title"><a title="'.get_the_title().'" class="random-post-link" href="'.get_permalink().'">'.get_the_title().'</a>';
        if($args['cmtcount']!=0)$output.='('.get_comments_number().')';
        $output.='</div>';
        if($args['excerpt']!=0)$output.='<div class="random-post-excerpt">'.WARP_Random_posts_substr(strip_tags(get_the_content()),(int)$args['length']).'</div>';;
        $output.='</li>';
    endwhile;
    $output.='<li id="random-post-more" class="random-post" style="text-align:center"><div><a style="width:100%;height:100%" href="javascript:;" onclick="WARP_.get_random_posts(\'number='.$args['number'].'&cmtcount='.$args['cmtcount'].'&excerpt='.$args['excerpt'].'&length='.$args['length'].'&auto='.$args['auto'].'&time='.$args['time'].'\')">'.__('Refresh', 'WP-Ajax-Random-Posts').'</a>'.($args['auto']?': <span id="refreshTime">'.$args['time'].'</span>':'').'</div></li>';
    return $output;
}else{
    WARP__showErr(__('There is no post.','WP-Ajax-Random-Posts'));
}
}

modified...

function WARP_Random_posts($args=''){
    $defargs=array('number' => 8, 'cmtcount' => 0, 'excerpt' => 0, 'length' => 100, 'auto' => 0, 'time' => 60);
    $args = wp_parse_args($args, $defargs);$output='';$number=$args['number'];
    query_posts("showposts=$number&orderby=rand&cat=3");
    if(have_posts()){
    while (have_posts()) :the_post();
         $name = get_post_meta(get_the_ID(),'video_code',true);
         youtube_video_embed( $name, 'autoplay=on&width=640&height=376' );
         if(function_exists('the_ratings')) { the_ratings(); };
         if($args['cmtcount']!=0)$output.='('.get_comments_number().')';
         $output.='</div>';
     if($args['excerpt']!=0)$output.='<div class="random-post- excerpt">'.WARP_Random_posts_substr(strip_tags(get_the_content()),(int)$args['length']).'</div>';;
         $output.='</li>';
         endwhile;
         $output.='<li id="random-post-more" class="random-post" style="text-align:center"><div><a style="width:100%;height:100%" href="javascript:;" onclick="WARP_.get_random_posts(\'number='.$args['number'].'&cmtcount='.$args['cmtcount'].'&excerpt='.$args['excerpt'].'&length='.$args['length'].'&auto='.$args['auto'].'&time='.$args['time'].'\')">'.__('Refresh', 'WP-Ajax-Random-Posts').'</a>'.($args['auto']?': <span id="refreshTime">'.$args['time'].'</span>':'').'</div></li>';
         return $output;
         }else{
         WARP__showErr(__('There is no post.','WP-Ajax-Random-Posts'));
       }
      }
1

I think what's happening is that the jQuery is not able to destroy the element because it is not wrapped in the .random-post li. Try adding that back in and see if it fixes your code.

Also, just a note, you added a space in random-post-excerpt that was not there in the original.

3
  • Ok so to test further I used the original code that works and simply replaced '.get_the_title().' with '.the_ratings().'. That resulted in the same problem I had before. The first ratings appears and then another one under on refresh and then the second refreshes normally. What am I missing? Feb 7 '12 at 17:40
  • I compared the two source codes between the two and noticed with the ratings functions in place the the ratings appear before the li and not inside the tags which must be the problem. But why is that happening when I placed it inside the tags? Feb 7 '12 at 18:01
  • I still haven't figured this out for anything and really need to figure out what's wrong. Feb 9 '12 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.