Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress使用查询作为AJAX_Php_Ajax_Wordpress - Fatal编程技术网

Php Wordpress使用查询作为AJAX

Php Wordpress使用查询作为AJAX,php,ajax,wordpress,Php,Ajax,Wordpress,我有一个自定义字段选择框,在这里我选择了要显示的帖子。现在,我希望查询(从中获取所选帖子及其自定义字段)以某种方式用作AJAX调用,以便我可以使用数据输出构建砖石网格。可能吗 以下是我的数据/查询: $posts = get_field('choose_artist'); if( $posts ): ?> <?php foreach( $posts as $p ): ?> <div class="artist"> <a href="&

我有一个自定义字段选择框,在这里我选择了要显示的帖子。现在,我希望查询(从中获取所选帖子及其自定义字段)以某种方式用作AJAX调用,以便我可以使用数据输出构建砖石网格。可能吗

以下是我的数据/查询:

$posts = get_field('choose_artist');

if( $posts ): ?>
<?php foreach( $posts as $p ): ?>
    <div class="artist">
        <a href="<?php echo get_permalink( $p->ID ); ?>">
                <div style="position: relative;">
                    <?php $image = get_field('artist_image', $p->ID); ?>
                    <div class="artist-image" style="background-image:url('<?php echo $image['url']; ?>')"></div>
                    <div class="overlay">
                        <div class="content">
                            <p><?php the_field('artist_name', $p->ID); ?></p>
                        </div>
                    </div>
                </div>
            </a>
    </div>
<?php endforeach; ?>
这可能吗?

ajax.php

     $content = '';
     if( $posts ) {
        foreach( $posts as $p ) {
            $image = get_field('artist_image', $p->ID);
            $content .= '<div class="artist">';
            $content .= '<a href="' . get_permalink( $p->ID ) . '">';
            $content .= '<div style="position: relative;">';
            $content .= '<div class="artist-image" style="background-image:url("' . $image['url'] . '")"></div>';
            $content .= '<div class="overlay">';
            $content .= '<div class="content">';
            $content .= '<p>' . get_the_field('artist_name', $p->ID). '</p>';
            $content .= '</div>';
            $content .= '</div>';
            $content .= '</div>';
            $content .= '</a>';
            $content .= '</div>';
        }   
     }
     echo $content;
查看文档,了解如何捕获错误、检查是否成功等:


有更好的方法来创建AJAX页面,因此需要对其进行重构并使其更整洁,但如果我了解您的需求,类似于上述的方法应该适用于您,尽管您可能需要对其进行调整以获得所需的内容。

是的,您做得对。。。回显您希望作为数据返回的脚本。@Dammeul您有示例吗?
     $content = '';
     if( $posts ) {
        foreach( $posts as $p ) {
            $image = get_field('artist_image', $p->ID);
            $content .= '<div class="artist">';
            $content .= '<a href="' . get_permalink( $p->ID ) . '">';
            $content .= '<div style="position: relative;">';
            $content .= '<div class="artist-image" style="background-image:url("' . $image['url'] . '")"></div>';
            $content .= '<div class="overlay">';
            $content .= '<div class="content">';
            $content .= '<p>' . get_the_field('artist_name', $p->ID). '</p>';
            $content .= '</div>';
            $content .= '</div>';
            $content .= '</div>';
            $content .= '</a>';
            $content .= '</div>';
        }   
     }
     echo $content;
$.ajax({
 url: '/ajax.php', //Path to the ajax.php script
 success: function(data){
     console.log(data);
 }});