如何获取最喜欢/最喜欢的作者总数

时间:2018-06-04 作者:Adham Mohamed

我正在尝试获取每个用户的最喜爱/最喜爱的总数,我计划将这些总数添加到author中。php来显示有多少作者喜欢他,有多少作者喜欢其他人,这是插件代码,那么如何才能只获得每个用户的计数?

http://wp.ohsikpark.com/favorite-authors/Description:收藏夹作者允许您将所有收藏夹作者添加到您的帐户中。作者:WriteGnVersion:1.2作者URI:http://www.ohsikpark.com文本域:收藏夹authorsLicense:GPL2*/已定义(\'ABSPATH\')或die(\'No script kiddies please!\');

//定义插件路径Define(\'FAV\\u AUTHORS\\u PATH\',plugins\\u url()。\'/最喜爱的作者“);定义(\'FAV\\u AUTHORS\\u META\\u KEY\',“收藏夹作者”);/***注册CSS(&A);JS assets for plug-in----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------注册我们的表单cssfunction fav\\u authors\\u register\\u assets(){wp\\u register\\u style(\'收藏夹authors\',fav\\u authors\\u PATH./收藏夹authors.css\');wp\\u register\\u style(\'主题名称样式,get\\u样式表uri(),数组(\'dashicons\'),\'1.0\',true);wp\\u register\\u脚本(“favorite authors script”,FAV\\u authors\\u PATH。“/favorite authors.js”,array(\'jquery\'),“1.0”,true);}add\\u action(\'init\',\'fav\\u authors\\u register\\u assets\');

//加载我们的表单cssfunction fav\\u authors\\u print\\u assets(){global$fav\\u authors\\u load\\u assets;

if ( !$fav_authors_load_assets )
    return;

wp_print_styles(\'favorite-authors\');
wp_print_styles(\'themename-style\');
wp_print_scripts(\'favorite-authors-script\');
}add\\u action(\'wp\\u footer\',\'fav\\u authors\\u print\\u assets\');

//加载并本地化ajax函数fav\\u authors\\u enqueue()的JS{wp\\u localize\\u script(\'favorite authors script\',\'fav\\u authors\\u ajax\\u object\',array(\'ajax\\u url=>admin\\u url(\'admin ajax.php\'),\'ajax\\u nonce=>wp\\u create\\u nonce(\'fav\\u authors\\u obj\\u ajax\');}添加\\u操作(“wp\\u enqueue\\u scripts”、“fav\\u authors\\u enqueue”);

//Get curren userfunction fav\\u authors\\u Get\\u user\\u id(){global$current\\u user;wp\\u Get\\u current\\u user();return$current\\u user->id;}

//将收藏夹作者添加到当前用户的usermetafunction fav\\u author\\u Add\\u fav\\u author(){检查\\u ajax\\u referer(\'fav\\u authors\\u obj\\u ajax\',security\');$添加\\u this\\u author=absint($\\u POST[\'clicked\\u author\\u id\');

if($add_this_author !== (int)$add_this_author)
    return;

if ( current_user_can( \'edit_posts\' ) ){
    $user_id = absint(fav_authors_get_user_id());

    $author_list = get_user_meta( $user_id, FAV_AUTHORS_META_KEY, true );

    if( empty( $author_list ) ) { 
        update_user_meta( $user_id, FAV_AUTHORS_META_KEY, array( $add_this_author ) );
    } else {
        $author_arr = ( is_array( $author_list ) ) ? $author_list : array( $author_list );

        if(in_array($add_this_author, $author_list))
            return;

        $author_arr[] = $add_this_author;
        update_user_meta( $user_id, FAV_AUTHORS_META_KEY, $author_arr );
    }
}
die();
}add\\u操作(“wp\\u ajax\\u get\\u fav\\u author\\u id”,“fav\\u author\\u add\\u fav\\u author”);

//从当前用户的usermetafunction fav\\u author\\u Remove\\u user(){检查\\u ajax\\u referer(\'fav\\u authors\\u obj\\u ajax\',\'security\');

if ($_POST[\'clicked_author_id\']){
    $remove_this_author = absint($_POST[\'clicked_author_id\']);

    if($remove_this_author !== (int)$remove_this_author)
        return;

    if ( current_user_can( \'edit_posts\' ) ){
        $user_id = absint(fav_authors_get_user_id());

        $author_list = get_user_meta( $user_id, FAV_AUTHORS_META_KEY, true );
        //print_r($author_list);
        $author_saved = array_search($remove_this_author, $author_list);

        if( FALSE !== $author_saved ){
            // Remove $author_saved
            unset($author_list[$author_saved]);
            $author_arr = ( is_array( $author_list ) ) ? $author_list : array( $author_list );
            update_user_meta( $user_id, FAV_AUTHORS_META_KEY, $author_arr );
        }
    }

}
die();
}add\\u操作(“wp\\u ajax\\u remove\\u fav\\u author\\u id”,“fav\\u author\\u remove\\u user”);

//在php文件函数fav\\u authors\\u link(){wp\\u enqueue\\u style(\'favorite-authors\')上添加shorcode;

if ( is_user_logged_in() ) {
    wp_enqueue_script(\'favorite-authors-script\');
    wp_enqueue_style(\'themename-style\');

    $fav_author_list = get_user_option( \'favorite-authors\', fav_authors_get_user_id() );
    //var_dump( $fav_author_list ); 

    global $post;
    //print_r($post);
    $user_id = absint($post->post_author);

    if(fav_authors_get_user_id() == $user_id)
        return;

    if( empty( $fav_author_list ) ){
        $str = \'<span class="fav_authors add-fav" id="fav_author_button" data-author-id="\'.$user_id.\'"><span class="dashicons dashicons-star-filled"></span> Favorite\'; 
    }else{

        if(in_array($user_id, $fav_author_list)){
            $str = \'<span class="fav_authors rmv-fav" id="fav_author_rmove_button" data-author-id="\'.$user_id.\'"><span class="dashicons dashicons-yes"></span> Favorited!\';
        }else{
            $str = \'<span class="fav_authors add-fav" id="fav_author_button" data-author-id="\'.$user_id.\'"><span class="dashicons dashicons-star-filled"></span> Favorite\';   
        }

    }
    $str .= \'</span>\';

    echo $str;
}else{
    echo \'<p class="fa-signin"><a href="\'.wp_login_url().\'" title="Login">Log in to favorite this author</a></p>\';
}
}

/***按当前用户为收藏夹作者列表分页此分页代码和获取收藏夹用户列表代码可以比此更好地清除*/函数fav\\u authors\\u pagi(){check\\u ajax\\u referer(\'fav\\u authors\\u obj\\u ajax\',\'security\');//var\\u dump($\\u POST);$fav\\u author\\u list=get\\u user\\u选项(\'favorite authors\',fav\\u authors\\u get\\u user\\u id());

$page = ! empty( $_POST[\'clicked_author_id\'] ) ? (int) $_POST[\'clicked_author_id\'] : 1;
$total = count( $fav_author_list ); 
$limit = 20; // <----------------------------------------------------- Set number of authors to show per page
$totalPages = ceil( $total/ $limit );
$page = max($page, 1);
$page = min($page, $totalPages);
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;


if ($fav_author_list){
    $fav_author_list = array_slice( $fav_author_list, $offset, $limit );

    foreach($fav_author_list as $fav_au ){
        $fa_au = get_userdata( $fav_au );
        $author_url = esc_url(site_url( "/author/" ).$fa_au->user_login);

        echo \'<li><a href="\'.$author_url.\'">\';
        echo esc_html($fa_au->display_name);
        echo \'</a> <span class="fav_authors rmv-fav" id="fav_author_rmove_button" data-author-id="\'.$fa_au->ID.\'"><span class="dashicons dashicons-yes"></span> Favorited!</span></li>\';
    }
}else{
    echo \'<h2>Add favorite authors!</h2>\';
}
die();
}add\\u action(\'wp\\u ajax\\u fav\\u au\\u pagi\',\'fav\\u authors\\u pagi\');

/***按当前用户快捷码获取收藏夹作者列表[收藏夹作者列表]函数*/函数fav\\u authors\\u Get\\u list(){$fav\\u author\\u list=Get\\u user\\u选项(\'收藏夹作者\',fav\\u authors\\u Get\\u user\\u id());//var\\u dump($fav\\u author\\u list);

$page = ! empty( $_POST[\'page\'] ) ? (int) $_POST[\'page\'] : 1;
$total = count( $fav_author_list );  
$limit = 20; // <----------------------------------------------------- Set number of authors to show per page
$totalPages = ceil( $total/ $limit ); 
$page = max($page, 1); 
$page = min($page, $totalPages);
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;

if ($fav_author_list){
    $fav_author_list = array_slice( $fav_author_list, $offset, $limit );

    echo \'<div class="fav_authors-wrap">\';
        echo \'<p class="fav-total">Total favorited author: \'.$total.\'</p>\';
        echo \'<div class="fav-authors-list" id="fav-authors-list">\';
        foreach($fav_author_list as $fav_au ){
            $fa_au = get_userdata( $fav_au );
            $author_url = esc_url(site_url( "/?author=" ).$fa_au->ID);

            echo \'<li><a href="\'.$author_url.\'">\';
            echo esc_html($fa_au->display_name);
            echo \'</a> <span class="fav_authors rmv-fav" id="fav_author_rmove_button" data-author-id="\'.$fa_au->ID.\'"><span class="dashicons dashicons-yes"></span> Favorited!</span></li>\';
        }
        echo \'</div>\';
        // Pagination
        echo \'<div class="fav-authors-pagination">\';

        if( $totalPages != 0 && $total >= $limit) {
            for ($i = $page; $i <= $totalPages; $i++) {
                if ($i == 1){
                    echo \'<li id="fav_pagi" data-fav-pid="\'.$i.\'" class="fa_current">\'.$i.\'</li>\';
                }else{
                    echo \'<li id="fav_pagi" data-fav-pid="\'.$i.\'">\'.$i.\'</li>\';
                }
            }
        }                   
        echo \'</div>\';
    echo \'</div>\';    
}else{
    echo \'<h2>Add your first favorite author!</h2>\';
}
}

/***为前端表单[收藏夹作者列表]添加短代码https://codex.wordpress.org/Function_Reference/add_shortcode*/函数fav\\u authors\\u list(){//加载CSS&JS文件全局$fav\\u authors\\u Load\\u assets;$fav\\u authors\\u Load\\u assets=true;

// Show only to logged in users
if ( is_user_logged_in() ) {
    $output = fav_authors_get_list();
    return $output;
}else{
    echo \'<p class="login-fav_authors">Please <a href="\'.wp_login_url().\'" title="Login">Login</a> to continue...</p>\';
}
}添加\\u短代码(\'favorite-authors-list\',\'fav\\u authors\\u list\');

//将插件链接添加到设置页面功能fav\\u authors\\u action\\u links($links){$links[]=“Documentation”;return$links;}添加过滤器(\'plugin\\u action\\u links\\uU\'。plugin\\u basename(FILE), \'fav\\u authors\\u action\\u links”);

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

这将帮助您显示当前用户最喜爱的其他作者的数量:

将此添加到主题功能中。php文件

<?php
function get_fav_auth_count(){
    if ( is_user_logged_in() ) {
        global $current_user;
        wp_get_current_user();
        $count = size_of(get_user_option(\'favorite-authors\', $current_user->ID));
        echo $count;
    }
}
?>
这样称呼它<?php get_fav_auth_count(); ?>

结束