加载更多同一类别的帖子-WordPress + Ajax + Timber

问题描述

我在WordPress中使用Timber。

我正在尝试使用const copiedArray = [3,2,1]; const ref = copiedArray; const swapped = copiedArray.sort(); console.log(copiedArray); console.log(ref); console.log(swapped);按钮创建帖子列表。

当用户单击按钮"Load more posts"

时,我想显示10个相同类别的帖子并加载10个其他相同类别的帖子

在类别之间没有区别时,一切都很好。按钮"Load more posts"工作正常。显示10个帖子。

但是,当我尝试单击按钮"Load more posts"来显示同一类别的帖子时。没有帖子显示。类别有什么问题?

请给我一些帮助吗?

archive.php

"Load more posts"

script.js

$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
));

functions.php

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($('<div></div>').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);
            }
        });
    });
}

archive.twig

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','paged' => $context['get_page'],'has_password' => FALSE
    ));
    $count = count(Timber::get_posts(array(
        'post_type' => 'post','posts_per_page' => -1,'category__in' => array($category)
    )));

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

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

    die();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)