2010-12-10 37 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