首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载相同类别的更多帖子- Ajax + Timber

加载相同类别的更多帖子- Ajax + Timber
EN

WordPress Development用户
提问于 2020-08-19 10:01:13
回答 1查看 2K关注 0票数 0

我在WordPress中使用Timber。

我正在尝试创建一个带有"Load more posts"按钮的帖子列表。

当用户单击"Load more posts"按钮时,我希望显示10个相同类别的帖子,并加载相同类别的其他10个帖子。

当没有类别之间的区别时,所有的工作都很好。按钮"Load more posts"工作正常。展示了10个员额。

但是,当我试图显示相同类别的文章时,我点击按钮"Load more posts"。没有张贴的帖子。类别有什么问题?

能帮我一下吗?

archive.php

代码语言:javascript
复制
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;

$context['posts'] = Timber::get_posts(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__in' => array($category),
    'posts_per_page' => 10,
    'paged' => 1,
    'has_password' => FALSE
));

script.js

代码语言:javascript
复制
function load_more_news() {
    var page = 1;

    $(document).on('click', '#load-more-news', function(e) {
        e.preventDefault();
        page++;

        $.ajax({
            type: 'POST',
            url: '/wp-admin/admin-ajax.php',
            dataType: 'html',
            data: {
                'action' : 'get_news',
                'get_page' : page
            },
            success: function(data) {
                if($('').html(data).find('.archive__item.ended').size() > 0) $('#load-more-news').parents('.cta').remove();
                else $('#load-more-news').parents('.cta').show();
                $('#archive__list').append(data);
            },
            error: function(data) {
                console.log(data);
            }
        });
    });
}

functions.php

代码语言:javascript
复制
add_action( 'wp_ajax_nopriv_get_news', 'get_news' );
add_action( 'wp_ajax_get_news', 'get_news' );
function get_news() {
    global $post;

    $context = Timber::get_context();
    $context['get_page'] = empty($_POST['get_page']) ? 1 : $_POST['get_page'];

    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;

    $context['posts'] = Timber::get_posts(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category__in' => array($category),
        'posts_per_page' => 10,
        'paged' => $context['get_page'],
        'has_password' => FALSE
    ));
    $count = count(Timber::get_posts(array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'category__in' => array($category)
    )));

    if($count <= $context['get_page'] * 10) $context['ended'] = 'ended';

    Timber::render( 'bloc_news.twig', $context );

    die();
}

archive.twig

代码语言:javascript
复制
    In the same thematic
    
        
            ...
        
    


{% if posts|length >= 10 %}

    
        Load more posts
    

{% endif %}
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2020-08-19 11:13:02

我认为在您的代码中有一个小的更正,您只是传递页码,但您也有传递类别ID。我在load按钮中添加了data-category属性。因此,您的存档.like应该是:

代码语言:javascript
复制
    In the same thematic
    
        
            ...
        
    


{% if posts|length >= 10 %}

    
        Load more posts
    

{% endif %}

通过script.js传递data-category属性值,因此script.js代码应该如下所示:

代码语言:javascript
复制
function load_more_news() {
    var page = 1;

    $(document).on('click', '#load-more-news', function(e) {
        e.preventDefault();
        page++;

        $.ajax({
            type: 'POST',
            url: '/wp-admin/admin-ajax.php',
            dataType: 'html',
            data: {
                'action' : 'get_news',
                'get_page' : page,
                'get_category' : $(this).data('category'),
            },
            success: function(data) {
                if($('').html(data).find('.archive__item.ended').size() > 0) $('#load-more-news').parents('.cta').remove();
                else $('#load-more-news').parents('.cta').show();
                $('#archive__list').append(data);
            },
            error: function(data) {
                console.log(data);
            }
        });
    });
}

现在在服务器端(如data-category )接收get_page属性值,因此您的functions.php代码应该如下所示:

代码语言:javascript
复制
add_action( 'wp_ajax_nopriv_get_news', 'get_news' );
add_action( 'wp_ajax_get_news', 'get_news' );
function get_news() {
    global $post;

    $context = Timber::get_context();
    $context['get_page'] = empty($_POST['get_page']) ? 1 : $_POST['get_page'];
    $context['get_category'] = isset($_POST['get_category']) ? $_POST['get_category'] : '';

    $context['posts'] = Timber::get_posts(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category__in' => array($context['get_category']),
        'posts_per_page' => 10,
        'paged' => $context['get_page'],
        'has_password' => FALSE
    ));
    $count = count(Timber::get_posts(array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'category__in' => array($context['get_category'])
    )));

    if($count <= $context['get_page'] * 10) $context['ended'] = 'ended';

    Timber::render( 'bloc_news.twig', $context );

    die();
}
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/373299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档