【问题标题】:Load more random posts on Wordpress using Ajax使用 Ajax 在 Wordpress 上加载更多随机帖子
【发布时间】:2014-08-25 15:47:01
【问题描述】:

我的 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();

【问题讨论】:

    标签: javascript php jquery ajax wordpress


    【解决方案1】:

    您可以将帖子返回的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2022-01-22
      • 2015-04-16
      • 2015-10-13
      • 2021-08-19
      • 2019-03-18
      相关资源
      最近更新 更多