Ajax页面模板中的WordPress回声功能使链接跳转到首页吗?

时间:2019-02-17 07:00:58

标签: php ajax wordpress echo

当我尝试在Ajax分页功能内回显功能时,会发生奇怪的事情!

要清楚:我在author.php中有一个关注者/关注用户的列表,然后我使用“加载更多”按钮为他们创建了ajax分页。

我正在使用“ Users Following System”插件,下面您可以找到显示跟随按钮链接echo pwuf_get_follow_unfollow_links( $user->ID );的功能

我以前在其他页面模板中使用了此模板,但在ajax中却没有使用它,并且没有任何问题!

但是当我尝试将其添加到ajax函数中无法正常工作时,当我单击(关注按钮)时,它跳转到页面顶部吗?

还尝试添加#!转到href="#!"链接,页面从跳到顶部停止,但是跟随按钮仍然不起作用!

控制台和调试很清楚!那我做错了吗?

这是我所有的代码,我希望找出问题所在,

请告诉我我是否想添加任何其他代码,谢谢!

display-functions.php的跟随按钮。

function pwuf_get_follow_unfollow_links( $follow_id = null ) {

    global $user_ID;

    if( empty( $follow_id ) )
        return;

    if( ! is_user_logged_in() )
        return;

    if ( $follow_id == $user_ID )
        return;

    ob_start(); ?>
    <div class="follow-links">
<?php
if ( pwuf_is_following( $user_ID, $follow_id ) ) {
    $classes = "unfollow";
    $text = "Following";
} else {
    $classes = "follow";
    $text = "Follow";
}
?>
<span><a href="#" class="<?php echo $classes; ?>" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span><?php echo $text; ?></span></a></span>
<img src="<?php echo PWUF_FOLLOW_URL; ?>/images/loading.svg" class="pwuf-ajax" style="display:none;"/>
    </div>
    <?php
    return ob_get_clean();
}

Ajax function.php

add_action('wp_ajax_user_following_by_ajax', 'user_following_by_ajax_callback');
add_action('wp_ajax_nopriv_user_following_by_ajax', 'user_following_by_ajax_callback');

function user_following_by_ajax_callback()  {

    check_ajax_referer('user_more_following', 'security');
    $paged = $_POST['page'];

    /// detect the author data with the name or with the author id
    $curauth = NULL;

    if( isset($_POST['author_name']) AND trim($_POST['author_name']) != '' ) {

        $curauth = get_user_by('slug', trim($_POST['author_name']) );

    } elseif( isset($_POST['author']) AND intval($_POST['author']) > 0 ) {

        $curauth = get_userdata( intval($_POST['author']) );    

    }

    if( !is_null($curauth) ) {

        $include = get_user_meta($curauth->ID, '_pwuf_following', true);

        if ( empty( $include ) ) {

    echo '<div class="no-follower">Not found followers yet <i class="fa fa-slack fa-1x" aria-hidden="true"></i></div>';

        } else {

            $args = array (
                'order' => 'DESC',
                'include'  => $include,
                'number'  => '20',
                'paged' => $paged
            );

    $wp_user_query = new WP_User_Query( $args );

    $users = $wp_user_query->get_results();

    foreach ( $users as $user ) {

        $avatar_size = 90;
        $author_profile_url = get_author_posts_url($user->ID);
        $profile = get_userdata($user->ID);

    echo '<div class="members-name-9"><a href="', $author_profile_url, '">' . $profile->first_name .'</a>';

    echo '<a href="', $author_profile_url, '">', $avatar , '</a>';

    echo pwuf_get_follow_unfollow_links( $user->ID );

    }

    echo '</div>';

    }

  } else {

      echo '<div class="no-follower">Not found followers yet <i class="fa fa-slack fa-1x" aria-hidden="true"></i></div>';
      }

    wp_die();
}

follow.js

jQuery(document).ready(function($) {
    /*******************************
    follow / unfollow a user
    *******************************/
    $( '.follow-links a' ).on('click', function(e) {
        e.preventDefault();

        var $this = $(this);

        if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
            alert( pwuf_vars.login_required );
            return;
        }

        var data      = {
            action:    $this.hasClass('follow') ? 'follow' : 'unfollow',
            user_id:   $this.data('user-id'),
            follow_id: $this.data('follow-id'),
            nonce:     pwuf_vars.nonce
        };

        $this.closest('.follow-links').find('img.pwuf-ajax').show();

        $.post( pwuf_vars.ajaxurl, data, function ( response ) {
            if ( response == 'success' ) {
                if ( $this.hasClass( 'follow' ) ) {;
                    $this.removeClass( 'follow' ).addClass( 'unfollow' );
                    $this.find( 'span' ).text( 'Unfollow' );
                } else {;
                    $this.removeClass( 'unfollow' ).addClass( 'follow' );
                    $this.find( 'span' ).text( 'Follow' );
                }
            } else {
                alert( pwuf_vars.processing_error );
            }
            $this.closest('.follow-links').find('img.pwuf-ajax').hide();
        });
    });
});

0 个答案:

没有答案