2010-12-10 62 views
0

我在页面上显示随机后循环。我想通过“刷新”链接来刷新通过ajax的循环内容。点击Wordpress ajax循环刷新

这可能吗?

这是我的循环:

  <ul id="content-inner" class="thumb-grid clearfix"> 
      <?php query_posts('posts_per_page=20&orderby=rand'); ?> 
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
       <li> 
        <a href="<?php the_permalink(); ?>"> 
         <img src="<?php echo $my_image_url = get('thumbnail'); ?>" alt="" /> 
         <span class="title"><?php the_title(); ?></span> 
         <span class="feat"><?php $articletags = strip_tags(get_the_tag_list('',', ',''));echo $articletags;?></span> 
        </a> 
       </li> 
      <?php endwhile;?> 
      <?php endif; ?> 
      <?php wp_reset_query(); ?> 
      </ul> 

感谢

回答

0

把随机码后在一个div(如果你还没有的话),并刷新使用jQuery该分区的内容...

像这样的东西应该工作(但我没有时间来检验)...

在你的页面引用JQuery的头,然后用JQuery.Ready先装入随机职位(F或初始页面加载):

<head> 

<script> /*...reference JQuery...*/ </script> 
<script> 
    jQuery(document).ready(function($) { 
    $("#randomdiv").html("<?php getRandomPost() ?>"); 
    }); 
</script> 

</head> 

<body> 
.... 

<div id="randomdiv">[placeholder text]</div> 
<a id="refresh" href="#">click</a> 

<!-- Then for the REFRESH: 
make sure this script occurs AFTER your div (above) --> 
<script> 
    $(function() { 
     $("#refresh").click(function(evt) { 
     $("#randomdiv").html("<?php getRandomPost() ?>"); 
     evt.preventDefault(); 
     }) 
    }) 
</script> 

</body> 

所以,把你的整个循环代码在一个叫getRandomPost()(或其它函数),并把它放在你的wordpress“的functions.php”文件...,然后就叫“ $( “#randomdiv”)HTML( “”);”。在你的页面的头部初始加载,然后在身体,因为我已经显示刷新...

+0

嗨,thxs - ive连接我​​使用的循环,我不知道它可以适应或不能使用它发布的代码? – Blackbird 2010-12-10 22:13:49

+0

看到我上面更新的答案,并让我知道如果这有效。谢谢! – prilldev 2010-12-10 22:37:13

+0

感谢您的帮助。我似乎得到错误“致命的错误:调用未定义的函数query_posts()”反弹,当我点击刷新链接....任何想法? – Blackbird 2010-12-10 22:45:47