使用AJAX在Auth.php上获取作者帖子

时间:2019-11-30 作者:mems

嗨,我正在尝试按作者在作者上显示文章。php,但我无法获取作者ID。域。com/author/user\\u 23/(显示该用户的帖子)

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;
            }
        });
    });
功能

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();
}
作者。php

<?php $curauth = (get_query_var(\'author_name\')) ? get_user_by(\'slug\', get_query_var(\'author_name\')) : get_userdata(get_query_var(\'author\')); ?>

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

著者php意味着您已经在查看作者的存档,那么为什么需要显示该作者的帖子呢?

无论如何,您可以从get_queried_object, 返回整个用户对象。

<?php

$curauth = get_queried_object();
if($curauth && isset($curauth->ID)){
    //...trigger ajax to pass ID via the data object
}
参考号:

  • How to get author ID when an author page is being viewed?
  • https://developer.wordpress.org/reference/functions/get_queried_object/

    例如:

    <?php
    /* 
    * This is inside your author.php file, based on this:
    * https://github.com/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...
    
    然后,您需要修改javascript以查找作者ID,将其包含在Ajax调用中,并将方法更改为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;
                }
            });
        });
    
    
    现在,您正在向ajax操作发布数据。您需要从那里获取作者ID:

    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();
    }
    
    以上内容应该可以让您开始学习,但没有安全性或转义变量,因此我绝对推荐以下链接供您进一步阅读:

相关推荐

如何检测哪个页面加载了ajax_Query_attachments_args?

我试图用分类法隐藏附件。我想在wp admin的帖子页面中隐藏它们。我不知道如何从ajax\\u query\\u attachments\\u args检测我所在的页面。我尝试使用pre\\u get\\u posts,但它似乎不会触发ajax\\u query\\u attachments\\u args。add_filter(\'ajax_query_attachments_args\', \'hide_attachment_with_taxonomy\', 50, 1); f