【问题标题】:WP call custom AJAX request (WP Query) within separate PHP fileWP 在单独的 PHP 文件中调用自定义 AJAX 请求(WP Query)
【发布时间】:2017-10-05 18:30:57
【问题描述】:

我试图在位于我的 WP 主题文件夹内的 PHP 文件中调用自定义 AJAX 函数,但是我无法让它检索输出。我认为问题在于将 WP 查询链接到主 WP 文件?

$.ajax({
url: "../../ajax-crew.php",
type: "POST",
data: {loadcrew: true},
etc etc etc ...

我的自定义 ajax-crew.php 文件位于我的主题文件夹中(当我简单地回显“test”时,我将输出检索到我的页面上,但是当我尝试使用 WP 查询时,它不会创建任何输出:

<?php include '../../../wp-load.php'; ?>
<?php
if (isset($_POST['loadcrew'])){
$args = array(

'post_type' => 'team-members',
'order'     => 'rand',
'posts_per_page' => 1
);
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); 
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'featured-400-400', true);
$index = $wp_query->current_post;

echo $thumb_url[0];
endwhile;
endif;
} // end if isset
?>

【问题讨论】:

    标签: javascript php ajax wordpress


    【解决方案1】:

    您不应该创建自己的 AJAX 处理程序 PHP 文件,这可能会带来安全风险。 WordPress 有自己的 AJAX 处理程序文件,可以连接到使用操作和过滤器,您应该使用它。这篇关于 WordPress Codex 的文章对如何使用它有完整的解释,以及前端和后端使用的示例:AJAX in Plugins

    【讨论】:

      【解决方案2】:

      你应该试试米兰的回答。但是,如果您想知道您的代码在哪里无法返回响应,您可以使用WP php console 转储变量(将您的代码移动到 function.php 并在您的 php 脚本中调用函数)...

      【讨论】:

        猜你喜欢
        • 2016-03-22
        • 1970-01-01
        • 1970-01-01
        • 2019-07-22
        • 1970-01-01
        • 2020-01-13
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        相关资源
        最近更新 更多