文章最后更新时间:
B2美化模块,自定义数量并随机获取用户来展示,每次刷新会随机更换背景图片,也可以调用户自己设置的背景,我这里调用的是随机,可以自己选择;如果用户发布了文章则优先展示最近发布的文章,没有发布过文章会获取评论的文章 ,如果都没有就会显示暂无动态。想要获取展示更多内容可以自己增加调用即可。这里只分享一个简单的样式。
首先打开b2主题或者子主题的根目录下打开 index.php 查找
在下面添加
然后到/Modules/Index/目录下新建Index_user.php (先在Modules目录下新建Index目录) 将下面的代码复制到 Index_user.php里保存
array(
'display_name',
'user_id'
),
'number'=>$count,
'offset'=>$offset
) );
$users_found = $users->get_results();
$total = $users->get_total();
$pages = ceil($total/$count);
?>
echo get_option('Micnt_Modular_Index')['Micnt_user_title'] ?>
';
foreach ($users_found as $user) {
$user_data = B2\Modules\Common\User::get_user_public_data($user->ID);
$user_lv = B2\Modules\Common\User::get_user_lv($user->ID);
$user_vip = isset($user_lv['vip']['icon']) ? $user_lv['vip']['icon'] : '';
$user_lv = isset($user_lv['lv']['icon']) ? $user_lv['lv']['icon'] : '';
//$tips = B2\Modules\Common\Comment::get_tips();
//$tips = $tips['title'] ? $tips['title'] : __('这家伙很懒,什么都没留下','b2');
$following = get_user_meta($user->ID,'zrz_follow',true);
$following = is_array($following) ? count($following) : 0;
$followers = get_user_meta($user->ID,'zrz_followed',true);
$followers = is_array($followers) ? count($followers) : 0;
$ids[] = $user->ID;
// 获取用户最近发布的两篇文章
$args = array(
'author' => $user->ID,
'post_type' => 'post',
'posts_per_page' => 2, //展示用户文章数量
'orderby' => 'date',
'order' => 'DESC'
);
$latest_posts = new WP_Query($args);
// 获取用户最近发布的评论
$args = array(
'user_id' => $user->ID,
'number' => 6, //展示用户评论文章数量
'status' => 'approve',
'type' => 'comment',
'orderby' => 'comment_date',
'order' => 'DESC',
);
$latest_comments = get_comments($args);
$background_image = get_random_background_image();
//如需调用B2用户中心背景图,请将下面的$background_image修改成$user_data['cover']
echo '-
文章
'.count_user_posts($user->ID,'post').'
评论
'.B2\Modules\Common\Comment::get_user_comment_count($user->ID).'
粉丝
'.$followers.'
关注
'.$following.'
TA的动态
';
if ($latest_posts->have_posts()) {
while ($latest_posts->have_posts()) {
$latest_posts->the_post();
$post_thumbnail = get_the_post_thumbnail( $post->ID, 'thumbnail' );
if (!$post_thumbnail) {
$post_thumbnail = '
';
}
$post_categories = get_the_category();
echo '
'.$post_thumbnail.''.get_the_title().'
';
foreach($post_categories as $category) {
echo ''.$category->name.'
';
}
echo '
';
}
} elseif ($latest_comments) {
echo '';
foreach ($latest_comments as $comment) {
$comment_post_id = $comment->comment_post_ID;
$comment_mi = get_comment_link($comment->comment_ID);
echo '';
}
echo '';
}else {
echo ''.__('暂无动态','b2').'';
}
echo '
';
}
echo '
';
?>
$ids
))
?>
上方$count = 8; 代表调用8个用户的信息,可自行更改,获取文章数也可以自己改,这里默认是2篇文章。
将下面的CSS文件放入主题的 style.css 里
打开主题根目录 functions.php 最下面加入
// 获取用户列表
function get_random_users() {
$users = get_users( array(
'orderby' => 'rand',
'number' => 8,
) );
return $users;
}
//每次刷新随机背景
function get_random_background_image() {
$images = array(
'b_1.jpg',
'b_2.jpg',
'b_3.jpg',
'b_4.jpg',
'b_5.jpg',
'b_6.jpg',
);
$image_url = B2_CHILD_URI.'/images/bg/' . $images[ array_rand( $images ) ];
return $image_url;
}
上方更改为自己的图片路径。
下载文件上传到主题或子主题目录;然后清空浏览器缓存就好了。
暂无评论内容