[英]Load more random posts on Wordpress using Ajax
我在Wordpress博客側邊欄上有5條隨機帖子。
我想創建一個按鈕,只要單擊該按鈕,ajax就會在其下加載5個其他隨機帖子。
問題是我不想多次獲得相同的隨機帖子。 我沒有絕對的計划,我可以保存帖子ID名稱或類似的名稱,但是我想知道是否有更好的有效方法。
有人可以幫我這個忙嗎?
這是我在側邊欄上加載的隨機帖子,加載7個帖子:
<?php
global $post;
$args = array( 'posts_per_page' => 7, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
$count_rand=0;
foreach( $rand_posts as $post ) : setup_postdata( $post ); ?>
<li><h2 class="r-h2"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h2><p><?php echo get_the_post_thumbnail($thumbnail->ID, 'medium'); //the_excerpt(); ?></a></p></li>
<?php endforeach;
wp_reset_postdata();
您可以將帖子返回的ID保存到WordPress瞬態系統( set_transient()
, get_transient()
等)中,以便跟蹤訪問者的文章結果。 然后,您可以將它們傳遞回查詢參數。
$exclude = get_transient( '__my_unique_articles_retrieved' );
$args = array(
'posts_per_page' => 7,
'orderby' => 'rand',
'post_status' => 'publish',
'offset' => 1,
'exclude' => $exclude
);
通過在此處將其排除,您將確保始終獲得7個帖子。 如果達到一定數量,也可以使用delete_transient()
重置它。
有關WordPress中瞬態的更多信息,請參見此處... http://codex.wordpress.org/Transients_API
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.