个性化阅读
专注于IT技术分析

WordPress帖子-Ajax加载更多随机顺序,过滤器不起作用?

我的功能目前在我的网站上加载更多帖子时遇到一些问题。默认情况下, 你会看到它以正确的顺序显示最新的四个帖子。但是, 当我单击”加载更多”按钮时, 它确实显示更多帖子, 但是, 它只是以随机顺序堆积。

我已经为此苦苦挣扎了近两天, 尝试了我遇到的所有问题, 并从我的朋友那里得到了帮助, 而我的朋友实际上是一位经验丰富的WordPress开发人员, 他也没有找到解决方案。

我做了一些研究, 并尝试添加’suppress_filters’= true, 但也没有任何运气。到目前为止, 这是我的代码。

这是显示正确顺序的默认四个帖子的代码:

<section class="posts-container">
        <div class="container">
            <h1 class="earlier-posts-title mt-5 text-center"><?php echo get_theme_mod('earlier_posts_title'); ?></h1>
            <div class="container-fluid">
                <div class="row mt-5 mb-5 misha_posts_wrap">

                    <?php if(have_posts()) : ?>
                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <div class="col-lg-6 mb-4">
                        <?php if(has_post_thumbnail()): ?>
                        <div class="thumbnail-container">
                            <?php the_post_thumbnail('', array('class' => 'thumbnail-post')); ?>
                        </div>
                        <?php endif; ?>
                        <h1 class="thumbnail-title mt-3 text-center"><?php the_title(); ?></h1>
                        <p><?php echo the_time(); ?></p>
                        <div class="meta">
                            <p class="thumbnail-meta mt-3 text-center"><i class="far fa-clock"></i> Skrevet den <?php the_time('j. F Y');?></p>
                        </div>
                            <p class="thumbnail-content-text text-left"><b class="text-left"><?php the_excerpt();?></b></p>
                        </div>
                        <?php endwhile; ?>
                        <?php endif; ?>
                        <?php endif;?>
                        <?php
                    global $wp_query; // you can remove this line if everything works for you
                    if (  $wp_query->max_num_pages > 1 )
                        echo '<div class="misha_loadmore">More posts</div>'; // you can use <a> as well
                ?>
                </div>
            </div>
        </div>
    </section>

这是myloadmore.js:

jQuery(function($){
    $('.misha_loadmore').click(function(){

        var button = $(this), data = {
            'action': 'loadmore', 'query': misha_loadmore_params.posts, 'page' : misha_loadmore_params.current_page
        };

        $.ajax({
            url : misha_loadmore_params.ajaxurl, data : data, type : 'POST', beforeSend : function ( xhr ) {
                button.text('Loading...');
            }, success : function( data ){
                if( data ) { 
                    button.text( 'More posts' ).prev().before(data);
                    misha_loadmore_params.current_page++;

                    if ( misha_loadmore_params.current_page == misha_loadmore_params.max_page ) 
                        button.remove(); // if last page, remove the button

                    // you can also fire the "post-load" event here if you use a plugin that requires it
                    // $( document.body ).trigger( 'post-load' );
                } else {
                    button.remove(); // if no data, remove the button as well
                }
            }
        });
    });
});

最后但并非最不重要的是, functions.php文件:

function misha_my_load_more_scripts() {

    global $wp_query; 

    // In most cases it is already included on the page and this line can be removed
    wp_enqueue_script('jquery');

    // register our main script but do not enqueue it yet
    wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/myloadmore.js', array('jquery') );

    // now the most interesting part
    // we have to pass parameters to myloadmore.js script but we can get the parameters values only in PHP
    // you can define variables directly in your HTML but I decided that the most proper way is wp_localize_script()
    wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
        'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
        'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
        'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1, 'max_page' => $wp_query->max_num_pages
    ) );

    wp_enqueue_script( 'my_loadmore' );
}

add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );

function misha_loadmore_ajax_handler(){

    // prepare our arguments for the query
    $args = json_decode(stripslashes($_POST['query']), true);
    $args['paged'] = $_POST['page'] + 1;
    $args['post_type'] = 'post';
    $args['order'] = 'DESC';
    $args['orderby'] = 'date';
    $args['post_status'] = 'publish';
    // it is always better to use WP_Query but not here
    query_posts( $args );

    if( have_posts() ) :

        // run the loop
        while( have_posts() ): the_post();

            // look into your theme code how the posts are inserted, but you can use your own HTML of course
            // do you remember? - my example is adapted for Twenty Seventeen theme
            echo '<div class="col-lg-6 mb-4">';
            echo '<div class="thumbnail-container">';
            the_post_thumbnail('', array('class' => 'thumbnail-post'));
            echo '</div>';
            echo '<h1 class="thumbnail-title mt-3 text-center">';
            echo the_title();
            echo '</h1>';
            echo '<div class="meta">';
            echo '<p class="thumbnail-meta mt-3 text-center"><i class="far fa-clock"></i> Skrevet den ';
            echo the_time('j. F Y');
            echo '</p>';
            echo '</div>';
            echo '<p class="thumbnail-content-text text-left"><b class="text-left">';
            echo the_excerpt();
            echo '</b></p>';
            echo '</div>';
            echo '</div>';

        endwhile;

    endif;
    die;
}

add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler');
add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler');

因此, 根据我的论文, 问题出在functions.php文件中的misha_loadmore_ajax_handler上, 因为一旦我按下”加载更多”按钮, 所有帖子就会以随机顺序进行排序。你还可以看到我已应用了许多过滤器来解决此问题。我只希望它能正确显示。

该代码来自Misha, 链接位于:https://rudrastyh.com/wordpress/load-more-posts-ajax.html

一般来说, 我对WordPress开发和PHP还是比较陌生。

我在这里做错了什么?任何帮助将不胜感激。


#1


仅使用’suppress_filters’= true而不在$ args数组中排序。.(如果需要在排序之前-使用pre_get_post)

$args = json_decode(stripslashes($_POST['query']), true);
$args['paged'] = $_POST['page'] + 1;
$args['post_type'] = 'post';
$args['post_status'] = 'publish';
$args['suppress_filters'] = true;

query_posts( $args );
赞(0) 打赏
未经允许不得转载:srcmini » WordPress帖子-Ajax加载更多随机顺序,过滤器不起作用?
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏