Get Author Post on author.php with AJAX
Question
Hi I’m trying to display post by author on author.php but I can’t get the author ID. domain.com/author/user_23/ ( display post from that user )
JQuery
jQuery('#menu_posts').click(function(){
$.ajax({
type : 'GET',
url : '/wp-admin/admin-ajax.php',
dataType : 'html',
data : { action: 'user_posts' },
success : function(data){
$('#user-tab').html(data);
},
error: function(data){
alert("Error!");
return false;
}
});
});
Function
add_action('wp_ajax_user_posts', 'user_posts');
add_action('wp_ajax_nopriv_user_posts', 'user_posts');
function user_posts() {
$args = array(
'author' => $curauth->ID,
'orderby' => 'asc',
'post_type' => 'post',
'posts_per_page' => 10
);
$get_posts_query = new WP_Query( $args );
while ( $get_posts_query->have_posts() ) : $get_posts_query->the_post() ?>
(( html here ))
<?php
endwhile;
die();
}
author.php
<?php $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author')); ?>
0
ajax, author
4 years
2019-11-30T05:03:02-05:00
2019-11-30T05:03:02-05:00 0 Answers
95 views
0
Leave an answer