Get posts with ajax

时间:2018-04-29 作者:wpdev

我想通过单击功能获得ajax帖子。

Jquery

$(".get-posts").click(function(){

  $.ajax({  
    type: \'POST\',  
    url: \'<?php echo admin_url(\'admin-ajax.php\');?>\',  
    data: { action : \'get_ajax_posts\' },  
    success: function(){  
       // ???
    }
  });  
});

Php

function get_ajax_posts() {
    // Query Arguments
    $args = array(
        \'post_type\' => array(\'products\'),
        \'post_status\' => array(\'publish\'),
        \'posts_per_page\' => 40,
        \'nopaging\' => true,
        \'order\' => \'DESC\',
        \'orderby\' => \'date\',
        \'cat\' => 1,
    );

    // The Query
    $ajaxposts = new WP_Query( $args );

    // The Loop
    if ( $ajaxposts->have_posts() ) {
        while ( $ajaxposts->have_posts() ) {
            $ajaxposts->the_post();
                get_template_part(\'products\');
        }
    } else {
        get_template_part(\'none\');
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}
add_action(\'wp_ajax_get_ajax_posts\', \'get_ajax_posts\');
add_action(\'wp_ajax_get_ajax_posts\', \'get_ajax_posts\');
我在这里被封锁了。

1 个回复
最合适的回答,由SO网友:kirillrocks 整理而成

选项1

简单方法-返回中的所有帖子JSON 在JavaScript中格式化和循环它们

PHP:

function get_ajax_posts() {
    // Query Arguments
    $args = array(
        \'post_type\' => array(\'products\'),
        \'post_status\' => array(\'publish\'),
        \'posts_per_page\' => 40,
        \'nopaging\' => true,
        \'order\' => \'DESC\',
        \'orderby\' => \'date\',
        \'cat\' => 1,
    );

    // The Query
    $ajaxposts = get_posts( $args ); // changed to get_posts from wp_query, because `get_posts` returns an array

    echo json_encode( $ajaxposts );

    exit; // exit ajax call(or it will return useless information to the response)
}

// Fire AJAX action for both logged in and non-logged in users
add_action(\'wp_ajax_get_ajax_posts\', \'get_ajax_posts\');
add_action(\'wp_ajax_nopriv_get_ajax_posts\', \'get_ajax_posts\');

JavaScript:

$.ajax({
    type: \'POST\',
    url: \'<?php echo admin_url(\'admin-ajax.php\');?>\',
    dataType: "json", // add data type
    data: { action : \'get_ajax_posts\' },
    success: function( response ) {
        $.each( response, function( key, value ) {
            console.log( key, value ); // that\'s the posts data.
        } );
    }
});
选项2获取HTML内容并将其打印到屏幕上。

PHP:

function get_ajax_posts() {
    // Query Arguments
    $args = array(
        \'post_type\' => array(\'products\'),
        \'post_status\' => array(\'publish\'),
        \'posts_per_page\' => 40,
        \'nopaging\' => true,
        \'order\' => \'DESC\',
        \'orderby\' => \'date\',
        \'cat\' => 1,
    );

    // The Query
    $ajaxposts = new WP_Query( $args );

    $response = \'\';

    // The Query
    if ( $ajaxposts->have_posts() ) {
        while ( $ajaxposts->have_posts() ) {
            $ajaxposts->the_post();
            $response .= get_template_part(\'products\');
        }
    } else {
        $response .= get_template_part(\'none\');
    }

    echo $response;

    exit; // leave ajax call
}

// Fire AJAX action for both logged in and non-logged in users
add_action(\'wp_ajax_get_ajax_posts\', \'get_ajax_posts\');
add_action(\'wp_ajax_nopriv_get_ajax_posts\', \'get_ajax_posts\');

JavaScript:

   $.ajax({
        type: \'POST\',
        url: \'<?php echo admin_url(\'admin-ajax.php\');?>\',
        dataType: "html", // add data type
        data: { action : \'get_ajax_posts\' },
        success: function( response ) {
            console.log( response );

            $( \'.posts-area\' ).html( response ); 
        }
    });
在第二个示例中,更改.posts-area 要打印的选择器。

这个console.log 只是您可以在控制台中看到AJAX调用返回的信息。你应该在完成后移除它。

结束

相关推荐

从AJAX‘wpcf7_mail_ent’挂钩调用公共静态方法

我正在尝试从“wpcf7\\u mail\\u sent”挂钩(未登录用户从前端提交的表单)调用操作。它不起作用了。如果我从钩子调用相同的方法,即连接到“save\\u post”钩子的方法,它会工作。我从管理面板更新帖子。代码示例:插件#1(此部分有效)function event_updated( $post_ID, $post, $update ) { /* $post_ID(int) - Post ID. $post(WP_Post) - Post obje