所以我试图在wordpress中创建一个按钮,用户可以按下一个按钮,让他们跟随他们喜欢的作者。 (不,我对buddypress不感兴趣)
到目前为止我所做的是使用Jon Masterson的Post Like System for WordPress http://hofmannsven.com/2013/laboratory/wordpress-post-like-system/中的代码,当用户喜欢这个帖子时,它几乎会增加+1 update_post_meta。
我遇到的问题是,如果你一直走到最底层,你会看到类似的系统使用update_post_meta给post meta的计数+1,这正是我需要的按下按钮。问题是我找不到函数update_author_meta来存储该特定作者的++ $ author_follow_count。如果您知道我存储和更新特定作者的关注计数的方法,或指向正确的方向,我们将不胜感激。
<?php
function follow_scripts() {
wp_localize_script( 'jk_like_post', 'ajax_var', array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ajax-nonce' )
)
);
}
add_action( 'init', 'follow_scripts' );
/**
* (2) Save follow data
*/
add_action( 'wp_ajax_nopriv_jk-author-follow', 'jk_author_follow' );
add_action( 'wp_ajax_jk-author-follow', 'jk_author_follow' );
function jk_author_follow() {
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
die ( 'Nope!' );
if ( isset( $_POST['jk_author_follow'] ) ) {
$author_id = $_POST['author_id']; // author id
$author_follow_count = get_author_meta( $author_id, "_author_follow_count", true ); // author follow count
if ( function_exists ( 'wp_cache_post_change' ) ) { // invalidate WP Super Cache if exists
$GLOBALS["super_cache_enabled"]=1;
wp_cache_post_change( $post_id );
}
if ( is_user_logged_in() ) { // user is logged in
$user_id = get_current_user_id(); // current user
$meta_AUTHORS = get_user_option( "_followed_authors", $user_id ); // author ids from user meta
$meta_USERS = get_author_meta( $author_id, "_user_followed" ); //user ids from author meta
$followed_AUTHORS = NULL; // setup array variable
$followed_USERS = NULL; // setup array variable
if ( count( $meta_Authors ) !=0 ) { // meta exists, set up values
$followed_AUTHORS = $meta_AUTHORS;
}
if ( !is_array( $followed_AUTHORS ) ) // make array just in case
$followed_AUTHORS = array();
if ( count( $meta_USERS ) !=0 ) { // meta exists, set up values
$followed_USERS = $meta_USERS[0];
}
if ( !is_array( $followed_USERS ) ) //make an array just in case
$followed_USERS = array();
$followed_AUTHORS['author-'.$author_id, "_user_followed"] = $author_id; // Add author id to user meta array
$followed_USERS['user-'.$user_id] = $user_id; // add user id to author meta array
$user_follows = count( $followed_AUTHORS ); // count user follows
// *** Where the snag is ****
if ( !AlreadyFollowed( $author_id ) ) { // follow the author
update_post_meta( $author_id, "_user_followed", $followed_USERS ); // Add user ID to author meta
update_post_meta( $author_id, "_author_follow_count", ++$author_follow_count ); // +1 count author meta
update_user_option( $user_id, "_followed_authors", $followed_Authors ); // Add author ID to user meta
update_user_option( $author_id, "_author_follow_count", $user_follows ); // +1 count user meta
echo $author_follow_count; // update count on front end
答案 0 :(得分:0)
帖子将自定义元信息作为一项功能,因此您可以添加自定义元值。据我所知,作者没有自定义元信息。
您可以创建一个新表,并编写自定义update_author_meta函数。