簡體   English   中英

WordPress的loadmore按鈕不起作用。 $ wp_query不返回上一次循環查詢

[英]Wordpress loadmore button does not work. $wp_query not returning last post loop query

-編輯1-

發現了一些新東西。 我將它們放在最上面,因為它們可能比下面的代碼更相關。

我已經重新運行了幾次腳本。 我現在實際上得到了不同的發現。 運行var_dump($wp_query->query); $the_query = new WP_Query($queryArgs); 在post循環的第一個渲染中,為我提供了循環渲染頁面的查詢變量。 用ajax調用它會重新運行代碼的相同部分,對嗎? 所以比它返回空。

我的想法:

  • 頁面被調用,運行funtions.php。
  • 運行wp_enqueue_script('rtt_scripts');的一部分wp_enqueue_script('rtt_scripts');
  • 這是它獲取當前$wp_query值的時刻。 頁面的值。
  • 比用post循環渲染頁面。
  • 這是post循環運行$the_query = new WP_Query($queryArgs);
  • 按下負載后,ajax會比調用它來重新運行post循環更多。 使用wp_enqueue_script('rtt_scripts');設置查詢變量

這讓我思考。 我是否以錯誤的順序運行代碼? Ajax的查詢變量是否在錯誤的時刻設置? 其他想法。 我應該專注於如何在ajax查詢變量的第一個發布循環中獲取查詢變量嗎?

-結束編輯-

我在Wordpress中無法加載更多按鈕。 下面的代碼是我現在擁有的基本代碼。 據我所知,這應該是一個有效的代碼:)問題是盡管這不起作用。

我的問題是我不知道從哪里開始調試。 我最近知道問題出在哪里:

rtt_loadmore_ajax_handler()還有就是VAR $queryArg當var_dumping了var $queryArg兩個rtt_post_grid()rtt_loadmore_ajax_handler()

它給出了不同的結果。 在這里,我期待同樣的結果。 在Ajax調用中,它返回當前渲染頁面的參數,而不返回該頁面上的后查詢的參數。

global $wp_query; 是問題嗎? 我怎么從這里走?

基本郵政編碼:

function rtt_post_grid()
{

    $queryArgs = Array(
        "post_type" => Array(
            'news',
            'agenda'
        ),
        'posts_per_page' => 4,
        'post_status' => 'publish',
        'paged' => 1
    );

    // post grid wrap
    echo '<div id="rtt_posts_wrap"  >';

        rtt_post_grid_query($queryArgs);

    echo '</div>';

    // load more button
    echo '<form>';
        echo '<button id="rtt_loadmore" class=" button">Load more post</button>  ';
        echo '<input type="hidden" name="action" value="loadmore" />'; // this line might be obsolete
    echo '</form>';
}

function rtt_post_grid_query($queryArgs)
{

    // The Query
    $the_query = new WP_Query($queryArgs);
    // The Loop
    if ($the_query->have_posts()) {
        echo '<ul>';
        while ($the_query->have_posts()) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
        echo 'no posts found';
    }
}

設置JS:

if(!has_action('rtt_post_grid_script_and_styles')) {
    add_action('wp_enqueue_scripts', 'rtt_post_grid_script_and_styles', 1);

    function rtt_post_grid_script_and_styles()
    {
        global $wp_query;

        wp_register_script('rtt_scripts', plugin_dir_url( __FILE__ ) . 'js/script.js', array('jquery'), time());
        wp_enqueue_script('rtt_scripts');

        wp_localize_script('rtt_scripts', 'rtt_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' => $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
            'max_page' => $wp_query->max_num_pages
        ));

        wp_enqueue_script('rtt_scripts');
    }
}

JS / Ajax:

jQuery(function($){

    $(window).ready(function() {

        $('#rtt_loadmore').click(function () {

            $.ajax({
                url: rtt_loadmore_params.ajaxurl,
                data: {
                    'action': 'loadmore', // the parameter for admin-ajax.php
                    'query': rtt_loadmore_params.posts, // loop parameters passed by wp_localize_script()
                    'page': rtt_loadmore_params.current_page, // current page
                },
                dataType: 'json',
                type: 'POST',

                beforeSend: function (xhr) {

                    $('#rtt_loadmore').text('Bezig met laden...'); // some type of preloader
                },
                success: function (data) {

                    if (data) {

                        $('#rtt_loadmore').text('More posts');

                        $('#rtt_posts_wrap').append(data.content); // insert new posts

                        rtt_loadmore_params.current_page++;

                        if (rtt_loadmore_params.current_page == rtt_loadmore_params.max_page){

                            $('#rtt_loadmore').hide(); // if last page, HIDE the button

                        }
                    } else {

                        $('#rtt_loadmore').hide(); // if no data, HIDE the button as well
                    }
                }
            });

            return false;
        });
    });
});

Ajax處理程序:

add_action('wp_ajax_loadmore', 'rtt_loadmore_ajax_handler'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore', 'rtt_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}

function rtt_loadmore_ajax_handler(){

    $postData = $_POST;

    // prepare our arguments for the query
    $queryArgs = json_decode( stripslashes( $postData['query'] ), true );
    $queryArgs['paged'] = $postData['page'] + 1; // we need next page to be loaded
    $queryArgs['post_status'] = 'publish';

    ob_start();

    rtt_post_grid_query($queryArgs);

    $output = ob_get_contents();

    ob_end_clean();

    global $the_query;

    echo json_encode( array(
        'posts' => serialize( $the_query->query_vars ),
        'max_page' => $the_query->max_num_pages,
        'found_posts' => $the_query->found_posts,
        'content' => $output
    ) );

    die;
}

將兩個順序參數添加到$ queryArgs。

// prepare our arguments for the query
$queryArgs = json_decode( stripslashes( $postData['query'] ), true );
$queryArgs['paged'] = $postData['page'] + 1; // we need next page to be loaded
$queryArgs['post_status'] = 'publish';

$queryArgs['orderby'] = 'date'; // add this to order by date
$queryArgs['order'] = 'DESC'; // add this to display the most recent

所以,我已經弄清楚了。 我會解釋,因為這可能對其他人有用。

它不起作用的原因是因為上面的代碼在模板中更有用。 但是我在短代碼中使用它。 wp_localize_script()在呈現頁面時運行,而不是在運行簡碼時運行。 這就是為什么它沒有正確的變量。

我已經將代碼移到了簡碼內。 在新的WP_query之后:

// The Query
$the_query = new WP_Query($queryArgs);

// The Loop
if ($the_query->have_posts()) {

    wp_enqueue_script_ajax_vars($the_query);

比通過新查詢

function wp_enqueue_script_ajax_vars($the_query)
{
wp_register_script('rtt_scripts', plugin_dir_url(__FILE__) . 'js/script.js', array('jquery'), time());

wp_localize_script('rtt_scripts', 'rtt_loadmore_params', array(
    'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
    'posts' => json_encode($the_query->query_vars), // everything about your loop is here
    'query_vars' => json_encode($the_query->query),
    'current_page' => $the_query->query_vars['paged'] ? $the_query->query_vars['paged'] : 1,
    'max_page' => $the_query->max_num_pages,
));

wp_enqueue_script('rtt_scripts', '', '', '', true); // note the last 'true' this sets it inside the footer
}

結果是wp_localize_script()在頁腳中創建了變量。 它在標題之前。 但是通過在短代碼中獲取它,發送新的查詢參數並將其放在頁腳中(因為此時標題已被渲染),我為Ajax設置了JS var。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM