【问题标题】:AJAX function in the widget class小部件类中的 AJAX 函数
【发布时间】:2013-08-31 14:16:20
【问题描述】:

我创建了一个 WordPress 小部件来获取最近的帖子,首先获取特定数量的帖子, 然后有一个按钮可以通过 AJAX 获取更多帖子。

完整的小部件代码

/* recent posts Widget
 ================================*/
 class recent_posts extends WP_Widget {

public function __construct() {
    // widget actual processes  
    parent::__construct(
        'recent_posts', // Base ID
        'recent posts', // Name
        array( 'description' => __( 'Show the recent posts', 'foo'))
         // Args
    );
    add_action('wp_ajax_ajaxloadMore', array($this,'ajaxloadMore'));
    add_action('wp_ajax_nopriv_ajaxloadMore', array($this,'ajaxloadMore'));
}

// i remove form and update functions to reduce the code :)
public function widget( $args, $instance ) {
    extract( $args );
    $title = $instance['title'];
    $number = $instance['num'];
    // function for filter posts list where first post is the current post 
    if(is_single()){
        function filter_where( $where = ''){
            $where .= " AND post_date <= '" .get_the_date('Y-m-d H:i:s'). "'";
            return $where;
        }
        add_filter( 'posts_where', 'filter_where' );
    }
    //select the current category ID
    $category = get_the_category($post->ID);
    $catid = $category[0]->cat_ID;
    query_posts('cat='.$catid.'&showposts='.$number.'');

    echo $before_widget;
    echo $before_title .$title.$after_title;
    echo '<ul class="recent_posts">';
    while(have_posts()) : the_post();
        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li><br>';
    endwhile;
    wp_reset_query();
    echo '</ul>';
    echo '<br><input type="button" id="loadMoreW" value="Load More" data-offset="'.$number.'">';
    echo $after_widget;
}
// the ajax function
public function ajaxloadMore(){
    // first need to get $catid and $number values from function widget
    // second need to declare filter_where function to filter the query_posts here
    query_posts('cat='.$catid.'&showposts='.$number.'&offset='.$_POST['offset'].'');

    while(have_posts()) : the_post();
        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li><br>';
    endwhile;
    wp_reset_query();
    die();
 }
}

这是获取ajaxloadMore函数输出并附加到ul标签的AJAX代码

$('#loadMoreW').on('click',function(){
    $.ajax({
        url: 'wp-admin/admin-ajax.php',
        type: 'POST',
        data: {
            action : 'ajaxloadMore',
            offset: $('#loadMoreW').data('offset') 
            },

        success: function(data){
            $('#loadMoreW').data('offset', $('#loadMoreW').data('offset')+4);
            $(data).appendTo('.recent_posts');
        }
    });

  });

如何从函数小部件中获取$catid$number变量并放入ajaxloadMore函数中?

如何在ajaxloadMore函数中过滤query_posts,就像函数小部件一样,通过相同的函数filter_where

【问题讨论】:

    标签: php jquery ajax wordpress widget


    【解决方案1】:

    两个问题的答案相同,使用与data-offset相同的方法:

    printf(
        '<br><input type="button" id="loadMoreW" value="Load More" data-offset="%s" data-catid="%s" data-where="%s">',
        $number,
        $catid,
        get_the_date('Y-m-d H:i:s')
    );
    

    然后

    $.ajax({
        url: '{$ajax_url}',
        type: 'POST',
        data: {
            action : 'ajaxloadMore',
            offset: $('#loadMoreW').data('offset') ,
            catid: $('#loadMoreW').data('catid') ,
            where: $('#loadMoreW').data('where')
            },
    

    最后在ajaxloadMore()函数中使用$_POST['catid']$_POST['where']

    我用$ajax_url = admin_url('admin-ajax.php'); 构建了$ajax_url

    不过,请尝试在 WordPress 中使用 AJAX,例如 thisdon't use query_posts

    【讨论】:

    • 很好,它适用于使用 query_posts ,当我使用 get_posts 时不起作用
    • WP_Query 怎么样?真的,不惜一切代价避免query_posts
    • 如果你使用这个小部件两次,如何得到这个值?例如在侧边栏和页脚中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多