如何发送字preSS类别的数据,阿贾克斯?类别、数据、preSS、阿贾克斯

2023-09-10 17:34:23 作者:╰花開ヽ蔠会落

我要发送的页面的当前类别阿贾克斯。我使用的Word preSS为我的博客网站。我想送当前页面类别infi.php,但我不知道如何做到这一点。

我的AJAX

  $。阿贾克斯({
        键入:POST,
        异步:假的,
        网址:/infi.php
        数据:{pcount:post_page_count},
        成功:
        功能(结果){
            $(#gizinfi)追加(结果);
            }
      });
 

解决方案 阿贾克斯2比1尤文 记住这些名字,他们是荷兰新一代巨星

要使用AJAX正确的Word preSS有你需要采取一些措施。

首先,假设您注册和正确的排入队列的JavaScript文件(如果你没有或不知道这是什么意思你应该看看如何排队在Word preSS文件),则需要本地化文件。在你的functions.php文件,你可以本地化的文件,像这样......

  $ data_array中=阵列(
    ajaxurl'=> ADMIN_URL(管理-ajax.php)
);

wp_register_script('YourAjaxScript',get_template_directory_uri()'JS / example.js',阵列('jQuery的'));
wp_localize_script('YourAjaxScript','myAjax',$ data_array中);
 

现在你需要从你的JavaScript访问类别ID的一些方法。你可以简单地包括一个空的跨度在模板中的某个地方,你的CATEGORY_ID存储作为数据的属性,那么你可以很容易地找到它使用JavaScript。您还可以添加一个随机数出于安全考虑,这可以让你检查,这是你的Ajax调用正在访问你的PHP,而不是randomer。因此,我们将添加到您的header.php ......

 < PHP
//为了这个,我们将只得到了第一类
$类别= get_the_category();
$猫=(!?空($类)$类[0]  - > term_id:假);

//我们也将创建一个随机数安全
$乱数= wp_create_nonce('ajax_nonce');
?>

<跨度ID =类别-ID数据类=< PHP的echo $猫;>中数据随机数=< PHP的echo $随机数;?>>< / SPAN>
 

现在你example.js文件,你可以创建你的AJAX功能......

  $(文件)。就绪(函数(){

    //获取您的数据变量
    变量$猫= $('#类-ID)的.data('类');
    变量$随机数= $('#类-ID)的.data('现时');

    $阿贾克斯({
        键入:POST,
        网址:myAjax.ajaxurl,
        数据: {
            动作:my_ajax_handler',// PHP函数来处理Ajax请求
            类别:猫,
            现时:$随机数
        },
        成功:功能(数据){
            $(#gizinfi)追加(数据);
        }
    });

});
 

然后你需要创建一个PHP函数来处理你的AJAX请求(可能只要进入你的infi.php文件中要包括该文件正确,但可能会在你的functions.php文件会更好)。例如...

  / **
 * my_ajax_handler  - 处理我的Ajax响应并返回一些数据
 * @返回字符串 - 我的数据
 * /
功能my_ajax_handler(){
    //首先,我们将验证了随机数和退出,如果不正确
    如果{退出(wp_verify_nonce($ _ POST ['随机数'],ajax_nonce')!); }

    //这里我们处理您的Ajax请求,并返回什么
    //我们所有的数据变量保存在$ _POST数组
    $类别= $ _ POST ['类'];
    返回$类;
}

add_action(wp_ajax_my_ajax_handler,my_ajax_handler);
add_action(wp_ajax_nopriv_my_ajax_handler,my_ajax_handler);
 

这些最后两行的功能绑定到你的Ajax调用。这应该是你所需要的。

希望帮助

I want to send current category of the page to ajax. I am using WordPress for my blog website. I want to send current page category to infi.php, But I don't know how to do this.

my ajax

$.ajax({
        type: "POST",
        async: false,
        url: "/infi.php",
        data: {pcount:post_page_count},
        success:
        function(result){
            $("#gizinfi").append(result);
            }
      });

解决方案

To use AJAX properly in Wordpress there are a few steps you need to take.

Firstly, assuming you are registering and enqueueing your javascript file properly (if you aren't or don't know what this means you should check out how to enqueue files in Wordpress), you need to localise the file. In your functions.php file you can localize a file like so...

$data_array = array(
    'ajaxurl' => admin_url( 'admin-ajax.php' )
);

wp_register_script( 'YourAjaxScript', get_template_directory_uri() . 'js/example.js', array('jquery') );
wp_localize_script( 'YourAjaxScript', 'myAjax', $data_array );

Now you need some way of accessing the category ID from your javascript. You could simply include an empty span in your template somewhere and store your category_id as a data attribute, then you can find it easily using javascript. You can also add a 'nonce' for security reasons, this allows you to check that it is your ajax call that is accessing your PHP, not a randomer. So we'll add this to your header.php...

<?php
//For the sake of this we'll only get the first category
$categories = get_the_category();
$cat = ( !empty( $categories ) ? $categories[0]->term_id : false );

//We'll also create a nonce for security
$nonce = wp_create_nonce( 'ajax_nonce' );
?>

<span id="category-id" data-category="<?php echo $cat; ?>" data-nonce="<?php echo $nonce; ?>"></span>

Now in your example.js file you can create your AJAX function...

$( document ).ready( function() {

    //Fetch your data variables
    var $cat = $( '#category-id' ).data('category');
    var $nonce = $( '#category-id' ).data('nonce');

    $.ajax({
        type: 'POST',
        url: myAjax.ajaxurl,
        data: {
            action: 'my_ajax_handler', //PHP function to handle AJAX request
            category: cat,
            nonce: $nonce
        },
        success: function( data ) {
            $("#gizinfi").append( data );
        }
    });

});

Then you need to create a PHP function that handles your AJAX request (could go in your infi.php file as long as you are including that file properly but may be better in your functions.php file). for example...

/**
 * my_ajax_handler - handles my ajax response and returns some data
 * @return string - my data
 */
function my_ajax_handler() {
    //First we'll validate the nonce and exit if incorrect
    if ( !wp_verify_nonce( $_POST['nonce'], 'ajax_nonce' ) ) { exit; }

    //Here we handle your ajax request and return whatever
    //All our data variables are saved in the $_POST array
    $category = $_POST['category'];
    return $category;
}

add_action("wp_ajax_my_ajax_handler", "my_ajax_handler");
add_action("wp_ajax_nopriv_my_ajax_handler", "my_ajax_handler");

Those last 2 lines bind the function to your ajax call. And that should be all you need.

Hope that helps

Dan