我的WordPress网站有5个作者级别。使用点计算标高
- level 1
- level 2
- level 3
- level 4
- level 5
作者根据这样的作者级别获得印象。
- Level l => 50%
- Level 2 => 55%
- Level 3 => 60%
- Level 4 => 65%
- Level 5 => 70%
到目前为止,我正在使用这样的代码。。
$random_number = mt_rand(1,100);
if ($author == \'level1\' && $random_number < 50) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level2\' && $random_number < 55) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level3\' && $random_number < 60) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level4\' && $random_number < 65) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level5\' && $random_number < 70) {
$adsenseid = \'author adsense id\';
} else {
$adsenseid = \'admin adsense id\';
}
上述代码取决于随机数。所以我认为这不是一种万无一失的方法。
有人能帮我以完美的方式做到这一点吗?可能使用usermeta
桌子
最合适的回答,由SO网友:PrivateUser 整理而成
function get_current_number($author_id) {
$current_number = (int) get_user_meta( $author_id, \'revshare_current_number\', true );
if( in_array( $current_number, range( 1, 99 ) ) ) {
$current_number++;
}
else {
$current_number = 1;
}
update_user_meta($author_id, \'revshare_current_number\', $current_number);
return $current_number;
}
$current_number = get_current_number($author_id);
if ($author == \'level1\' && $current_number < 50) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level2\' && $current_number < 55) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level3\' && $current_number < 60) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level4\' && $current_number < 65) {
$adsenseid = \'author adsense id\';
} elseif ($author == \'level5\' && $current_number < 70) {
$adsenseid = \'author adsense id\';
} else {
$adsenseid = \'admin adsense id\';
}