admin管理员组文章数量:1406951
Hi I'm trying to display post by author on author.php but I can't get the author ID. domain/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')); ?>
Hi I'm trying to display post by author on author.php but I can't get the author ID. domain/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')); ?>
Share
Improve this question
asked Nov 30, 2019 at 11:03
memsmems
7312 bronze badges
1 Answer
Reset to default 1Author.php means you are looking at an archive of the author already, so why would you need to display posts by that author?
Regardless, you can get the author ID from get_queried_object
, which returns the whole user object.
<?php
$curauth = get_queried_object();
if($curauth && isset($curauth->ID)){
//...trigger ajax to pass ID via the data object
}
Reference:
- How to get author ID when an author page is being viewed?
- https://developer.wordpress/reference/functions/get_queried_object/
Edit
You will need to amend your author.php file. I would recommend storing the author ID as a data component so your script can easily pick it up.
For example:
<?php
/*
* This is inside your author.php file, based on this:
* https://github/WordPress/WordPress/blob/master/wp-content/themes/twentyfourteen/author.php
* Yours may differ.
*/
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) :
$curauth = get_queried_object();
?>
<header class="archive-header" <?php echo sprintf('data-author_id="%s"'), ($curauth && isset($curauth->ID) ? $curauth->ID : 0 ) ?>>
<h1 class="archive-title">
//...the rest of the file below...
Then, you will need to amend your javascript to look for the author ID, include it in the Ajax call and change the method to POST:
jQuery('#menu_posts').click(function(){
var $curauth = $('[data-author_id]').data('author_id');
$.ajax({
type : 'POST',
url : '/wp-admin/admin-ajax.php',
dataType : 'html',
data : { action: 'user_posts', author_id: $curauth },
success : function(data){
$('#user-tab').html(data);
},
error: function(data){
alert("Error!");
return false;
}
});
});
You are now POSTing data to the ajax action. You will need to grab the author ID from there:
add_action('wp_ajax_user_posts', 'user_posts');
add_action('wp_ajax_nopriv_user_posts', 'user_posts');
function user_posts() {
// You should add security here, like check_ajax_referer
$curauth = (isset($_POST['author_id']) ? $_POST['author_id'] : false;
$args = array(
'author' => $curauth,
'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;
wp_die();
}
The above should get you started, but there is no security or escaping variables, so I would definitely recommend the following links for further reading:
- https://premium.wpmudev/blog/using-ajax-with-wordpress/
- https://medium/techcompose/how-to-use-ajax-precisely-in-wordpress-custom-themes-dc61616720a8
- https://codex.wordpress/WordPress_Nonces
- https://developer.wordpress/reference/functions/wp_localize_script/
本文标签: Get Author Post on authorphp with AJAX
版权声明:本文标题:Get Author Post on author.php with AJAX 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744953984a2634248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论