Products
GG网络技术分享 2025-03-18 16:13 0
我们在开发wordpress主题时,常常会用到获取用户信息的变量,下面我们说一下比较常用的
1、$current_user
这是一个全局变量,在使用的地方global $current_user即可获取到用户信息,它是由get_currentuserinfo函数赋值而来。
此外还有一些单独的全局用户变量例如$display_name, $user_email等。
2、wp_get_current_user()
检索当前登录用户的相关信息,并将信息置入$userdata全局变量
同样也将个体属性放置到以下独立全局变量中(意思是下面的变量可以单独拿出来使用,并且是全局的)
$user_login
$user_level
$user_ID
$user_email
$user_url(用户在用户资料中输入的网址)
$user_pass_md5 (用户密码的md5)
$display_name(用户名)
3、get_current_user_id()
直接用来获取当前登陆用户的ID
最近又wp自学笔记官网大改版,突然想把之前博客调用置顶文章的区块改成调用指定ID文章,于是便寻思着如何实现。
在网上搜了搜,发现利用WordPress函数get_post(),能够调用指定ID的文章链接、标题及内容。
下面是调用文章ID为1的文章:
<?php$post_id = 1; // 文章IDecho get_post( $post_id )->post_content; // 输出文章的内容echo get_post( $post_id )->post_title; // 输出文章的标题?>
下面是调用文章ID为1,2,3的文章:
<?php $postsl = get_posts(\\\"numberposts=4&post_type=any&include=1,2,3\\\"); if($postsl) : foreach( $postsl as $post ) : setup_postdata( $post ); ?><li><a href=\\\"<?php the_permalink(); ?>\\\" rel=\\\"external nofollow\\\" ><?php the_title();?></a></li><?php endforeach; endif; ?>
扫码关注wpwp自学笔记
精选优质免费WordPress主题模板,分享最新WordPress实用建站教程!
记住我们的网址:ztJun.com
Demand feedback