简体   繁体   English

如何将 php 变量传递给 Wordpress AJAX 调用?

[英]How to pass a php variable to an Wordpress AJAX call?

I have embeded a post (CPT) by a shortode.我通过短篇嵌入了一个帖子(CPT)。 So I get the output of this post on a page, where I want to filter the content of this embeded post by AJAX.所以我在一个页面上得到这篇文章的输出,我想通过 AJAX 过滤这个嵌入文章的内容。 That would work, if I could send the ID of this embeded post in the ajax call.如果我可以在 ajax 调用中发送这个嵌入帖子的 ID,那就行了。

I get the ID of the embeded post from a shortcode [documentlist listid="2126"].我从短代码 [documentlist listid="2126"] 中获得了嵌入帖子的 ID。

$atts = shortcode_atts(
        array(
            'listid' => '',
        ), $atts
    );
$posts = array(
    'post_type' => 'documentlist',
    'page_id'         => $atts['listid'],
);

So I have this $atts['listid'] variable.所以我有这个 $atts['listid'] 变量。 How can I add this to the following ajax code (which is in an diffeent scripts.js file)?如何将其添加到以下 ajax 代码(位于不同的 scripts.js 文件中)?

/*The ajax call*/
jQuery(function($){
    $('#filter #documenttypefilter, #filter #applicationareasfilter').change(function(){
        var filter = $('#filter');
        var serializedFilter = filter.serialize();
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(),  // form data
            type:filter.attr('method'), // POST

            success:function(data){
                $('#response').html(data); // insert data
            }
        });
        return false;
    });
});

Thanks so much for your help!非常感谢你的帮助!

I found something close to here: How to pass php variable to wordpress AJAX handler我在这里找到了一些接近的东西: How to pass php variable to wordpress AJAX handler

Get your PHP variable as below获取您的 PHP 变量,如下所示

var php_var = "<?php echo $atts['listid']; ?>";

Now use it in your AJAX call现在在你的 AJAX 调用中使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM