【问题标题】:Wordpress Load More Posts onclick with ajax request jqueryWordpress 加载更多帖子 onclick 与 ajax 请求 jquery
【发布时间】:2015-11-30 12:56:37
【问题描述】:

我使用 _S 入门主题从头开始开发自定义主题。我在单击阅读更多按钮时通过 ajax 请求获取 Wordpress 下一篇文章时遇到问题。我已经尝试了很多文章,特别是以下文章,但没有运气。

参考链接:

我尝试过使用上述自定义循环并使用 jquery 脚本添加自定义函数,但它无法以某种方式工作。

下面是循环代码示例:

在 index.php 中循环

<?php query_posts('showposts=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>

    <article class="post-item">
        <h2><?php the_title(); ?></h2>
        <?php the_excerpt(); ?>
    </article>

<?php endwhile; ?>
<?php else : ?>

    <h2>Sorry no posts are created yet.</h2>
    <p>Please create some posts to see it in action.</p>

<?php endif; wp_reset_query(); ?>

<button class="load-more-btn">Load More</button>

我已经解决这个问题超过 4-5 天了,所以任何人都可以通过有效的解决方案帮助我解决这个问题。

提前致谢。

【问题讨论】:

  • 如果您不介意为此使用插件:stackoverflow.com/questions/20167896/…
  • 您好 Clyff 感谢您的回复。我已经尝试过这个插件,甚至试图修改它以使其工作,但是通过这样做,我的“加载更多”按钮消失了,因为我将“posts_per_page”设置为 5,并且我的 wp 中有超过 5 个帖子。
  • 插件名称为 PBD Ajax Load Posts
  • 我也不想使用无限循环,因为它会通过转到页面底部来自动触发。相反,我更喜欢在点击时触发它。

标签: php jquery ajax wordpress twitter-bootstrap-3


【解决方案1】:

我也许有适合你的解决方案。

首先确保您的主题中有一个脚本队列

wp_enqueue_script('your_js_hanlde', get_template_directory_uri() . '/js/your_js_hanlde.js', array('jquery'), '1.0.0', true );

然后本地化添加一个函数来在你的 dom 中添加一个 js var

wp_localize_script('your_js_hanlde', 'ajaxurl', admin_url( 'admin-ajax.php' ) );

在您的 js 中,在“加载更多”按钮上的“点击”上添加一个事件

传递一个动作名称和您在 dom 中的文章数量,响应在您的按钮“加载更多”之前添加内容

$("#load_more").click(function()
{
    $.post(ajaxurl,
    {
      'action': 'your_load_more',
      'count': $("article.post-item").length
    },
    function(response)
    {
      var posts = JSON.parse(response);

      for( var i = 0; i < posts.length; i++ )
      {
        if( posts[i] == "0" )
          $("#load_more").fadeOut();
        else
          $("#load_more").before(posts[i]);
      }

    });
});

在你的functions.php中创建一个函数

function your_load_more()
{
    $count = $_POST["count"];

    $cpt = 1;

    $args = array(
        'posts_per_page' => -1,
        'post_type'   => 'post', // change the post type if you use a custom post type
        'post_status' => 'publish',
    );

    $articles = new WP_Query( $args );

    $ar_posts = array();

    if( $articles->have_posts() )
    {
        while( $articles->have_posts() )
        {
            $articles->the_post();

            $one_post = "";

            if( $cpt > $count && $cpt < $count+6 )
            {
                $one_post .= "<article id='" . get_the_id() . "' class='post-item'>";
                $one_post .= "<h3>" . get_the_title() . "</h3>";
                $one_post .= "</article>";

                $ar_posts[] = $one_post;

                if( $cpt == $articles->found_posts )
                    $ar_posts[] = "0";
            }
            $cpt++;
        }
    }
    echo json_encode($ar_posts);
    die();
}
add_action( 'wp_ajax_your_load_more', 'your_load_more' );
add_action( 'wp_ajax_nopriv_your_load_more', 'your_load_more' );

它对我有用。 希望对你有所帮助。

【讨论】:

  • 谢谢,它确实对我有用。虽然我需要加载更多功能,如 post_thumbnail、the_excerpt、the_permalink 和 the_post_metas 等。
  • 你可以轻松做到,改变循环内容
  • 一切都按照要求完成了再次感谢,但仍然存在一个小问题,如果循环中没有更多帖子,我如何禁用或隐藏“加载更多”按钮。
  • 我改变了我的功能。在数组中传递帖子并检查 loopt 中的当前帖子是否是最后一个。在这种情况下,使用自定义值(例如:0)在数组中添加最后一项,然后以 json 格式返回数组编码。在您的 js 函数中,解析数组并检查当前项是否等于“0”。隐藏按钮。为我工作。
  • 感谢您更新脚本,也再次感谢您共同努力帮助我。它真的很明显。让我用我的主题尝试一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2015-10-13
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2016-06-08
  • 2015-04-16
相关资源
最近更新 更多