javascript - 使用 AJAX 隐藏 'show more' 按钮

标签 javascript php jquery ajax wordpress

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

functions.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');

load.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();

    });

关于javascript - 使用 AJAX 隐藏 'show more' 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30886098/

相关文章:

具有多个规则的 jQuery 表单验证插件

javascript - 选择执行 :not have a parent 的 jQuery 元素

javascript - 根据父位置滚动 div

javascript - 是否可以对 JQuery 中文本区域中输入的文本进行实时检测?

java - 我缺少什么来获得 Java 中的 PHP ScriptEngine

php - 如何在 dropzone 上传后立即更新文件名?

javascript - 在同一按钮上调用多个函数对我来说不起作用(ajax)

javascript - jQuery UI selectmenu - 如何将选项附加到 optgroup

javascript - 获取数组值 - JQuery

php - Centos 7 Apache 和 PHP5 错误 : Package: php-5. 6.40-21.el6.remi.x86_64 (remi-php56)