使用 AJAX 隐藏“显示更多”按钮


Hide 'show more' button using AJAX

我的WP网站上有"显示更多"按钮。默认情况下显示 2 个帖子,单击此按钮会显示其他 2 个帖子。所以我需要检查页面加载,如果有超过 2 个帖子。它没有,按钮隐藏。我为此使用 AJAX。但是我的代码不起作用。

函数.php

function buttonHide() {
   $posts_count = wp_count_posts()->publish;
   if ($posts_count <= 2) {
    echo '<script>function hideLoadMore(){$("#load-post").hide();}</script>';
     }
     // Reset Query
    wp_reset_query();
    die();
}
add_action('wp_ajax_buttonHide', 'buttonHide');
add_action('wp_ajax_nopriv_buttonHide', 'buttonHide');

加载.js

$(function() {
        checkLoadButton();
        function checkLoadButton() {
            $.ajax({
                url: "/wp-admin/admin-ajax.php",

                data: ({               
                     action: 'buttonHide'
                }),
                type: "GET",
                success: function() {
                    if (hideLoadMore) {
                        hideLoadMore(); //function from functions.php
                    }
                }
            });
        }
    });

检查这个

函数.php

function buttonHide() {
   $posts_count = wp_count_posts()->publish;
   if ($posts_count <= 2) {

      $result['type'] = "success";
       $result['status'] = 'hide'  ;
       $result = json_encode($result);
        echo $result;
        die();
     }else{
        $result['type'] = "success";
        $result['status'] = 'show'  ;
        $result = json_encode($result);
        echo $result;
        die();
     }

    die();
}
add_action('wp_ajax_buttonHide', 'buttonHide');
add_action('wp_ajax_nopriv_buttonHide', 'buttonHide');

add_action( 'init', 'my_script_enqueuer' );
function my_script_enqueuer() {
   wp_localize_script( 'more_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        
   wp_enqueue_script( 'jquery' );
   wp_enqueue_script( 'more_script' );
}

加载.js

 function checkLoadButton() {
             jQuery.ajax({

            type : "post",
            dataType : "json",
            url : myAjax.ajaxurl,
            data : {action: "buttonHide"},

             success: function(response) {
            if(response.type == "success") {

                if(response.status=="hide"){
                    jQuery("#load-post").hide();
                }else if(response.status=="show"){
                    jQuery("#load-post").show();
                }
            }
            else {
               alert("Error")
            }
         }
            });
        }
$(function() {
        checkLoadButton();
    });

你的变量hideLoadMore确实没有定义。

如果你希望这个变量是对你的 Ajax 的响应,你应该更正你的回调函数:

$(function() {
    checkLoadButton();
    function checkLoadButton() {
        $.ajax({
            url: "/wp-admin/admin-ajax.php",
            data: ({               
                 action: 'buttonHide'
            }),
            type: "GET",
            success: function(response) {
                if (response) {
                    hideLoadMore(); //function from functions.php
                }
            }
        });
    }
});

如果你想得到一个函数作为结果,你必须在 AJAX 中使用 JSONP 作为 accept: -option