WordPress 全局变量


WordPress 内置全局变量 如 $post、$page、$paged和$pages
全局变量 用 global 声明 global $variable;
WordPress 全局变量也可以通过函数来获取
WordPress 官方推荐 函数的方式来获取 全局变量
global $wp_version; //方法1 提前用global声明
//或者$wp_version = get_bloginfo('version');  //方法2 用对应函数获取
echo $wp_version;
get_query_var() 只取WP_Query 可识别的公共查询参数
自定义查询参数 直接通过 get_query_var() 函数获取不了
需要使用 query_var  过滤器添加自定义查询参数
查看已经注册的 全局变量
echo "<pre>";
print_r($GLOBALS);
echo "</pre>";
数据库全局变量$wpdb
WordPress 主题或插件文件用wpdb函数
直接声明该全局变量即可
根目录或非 WordPress 环境用WordPress核心数据库操作功能
需要调用WordPress根目录的 wp-blog-header.php 配置文件
//根据具体需要设定调用目录
require_once('./wp-blog-header.php');
global $wpdb;//声明全局变量$wpdb
$wpdb->insert( $wpdb->prefix . 'fanly', array( 'name' => 'Fanly', 'url' => 'wangejiba.com' ) );// wpdb 插入数据
$wpdb->query( "DELETE FROM " . $wpdb->prefix . "fanly WHERE 'url' = 'wangejiba.com'" );// wpdb 删除数据
$wpdb->update( $wpdb->prefix . 'fanly', array( 'name' => 'Fanly', 'url' => 'wangejiba.com' ), array( 'name' => '玩机' ) );// wpdb 修改数据
$data = $wpdb->get_row( "SELECT * FROM ".$wpdb->prefix."fanly WHERE 'url'='wangejiba.com'" );//wpdb 获取数据
echo $data->name;
WordPress主要全局变量
WordPress 全局变量已经被提前获取了
$post (WP_Post) //当前文章对象
$authordata (WP_User) //当前文章作者对象
$currentday (string) //当前文章的发布日期
$currentmonth (string) //当前文章的发布月份
$page (int) //当前文章被访问的分页 通过查询参数 page 定义
$paged //当前的分页数值 主页 博客页面 存档页面和页面以计算分页 第一页是0
$pages (array) //当前文章的分页信息 每个分页元素包含了 --nextpage--标签分隔的部分
$multipage (boolean) //当前文章是否为多页文章 根据上面的 $pages 参数检测
$more (boolean) WordPress //是否执行 --more-- 标签的标记 如果为 true WordPress 将不会执行 more 标签
$numpages (int) //返回当前文章的总页数 和上面的 $pages x相关
What is the difference between $paged and $page?
<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}
query_posts('paged='.$paged.'&posts_per_page=');
?>
The main query uses WP_Query to set itself up. In the main query, WP_Query uses public query variables to construct the main query according to the page being requested, and paged and page are two of them. To see all the public query variables, paste this in your header and check on all templates how this are set according to the queried page
var_dump($wp_query->query_vars);
The function get_query_var() is used to get the values from those public query variables, and in this case that is page and paged.
To answer your question, these two parameters and its values are used by WP_Query to calculate pagination and more importantly the offset of posts according to page numbers. It is this parameter that ensures that your posts page correctly when paging through pages
paged -> Used on the homepage, blogpage, archive pages and pages to calculate pagination. 1st page is 0 and from there the number correspond to the page number
page -> Use on a static front page and single pages for pagination. Pagination on these pages works the same, a static front page is treated as single page on pagination. By pagination on single pages, I mean that single posts can be broken down into multiple pages
What your code basically does is, it checks if the paged parameter is set, if that fails, it checks whether the page parameter is set, and if that is not set, set the page to 1. This check will always fail on page 1, so $paged will always fall back to 1. On any other page than page one, either paged or page will return true and sets $paged to the correct page number
Never ever use query_posts unless you need to break something on your page, and believe you me, you don't want that. Always use WP_Query for paginated custom queries.

检测浏览器的布尔值
全局变量存储 用户浏览器的检测信息  布尔值 用户用来访问网站的浏览器
$is_iphone (boolean) //iPhone Safari
$is_chrome (boolean) //Google Chrome
$is_safari (boolean) //Safari
$is_NS4 (boolean) //Netscape 4
$is_opera (boolean) //Opera
$is_macIE (boolean) //Mac Internet Explorer
$is_winIE (boolean) //Windows Internet Explorer
$is_gecko (boolean) //FireFox
$is_lynx (boolean)
$is_IE (boolean) //Internet Explorer
$is_edge (boolean) //Microsoft Edge
检测网站服务器的布尔值
全局变量存储着网站服务器的一些信息
可以用来判断运行网站的服务器类型
$is_apache (boolean) //Apache HTTP Server
$is_IIS (boolean) //Microsoft Internet Information Services (IIS)
$is_iis7 (boolean) //Microsoft Internet Information Services (IIS) v7.x
$is_nginx (boolean) //Nginx web server
版本变量 存储 WordPress  版本信息
$wp_version (string) //当前安装的 WordPress 版本
$wp_db_version (int) //当前数据库的版本
$tinymce_version (string) //TinyMCE 的版本
$manifest_version (string) //缓存 manifest 的版本
$required_php_version (string) //网站安装的 WordPress 版本需要的最小 PHP 版本
$required_mysql_version (string) //网站安装的 WordPress 需要的最小 MySQL 版本
其他全局变量
$super_admins (array) //超级管理员权限的用户 ID, 此全局变量只对站点所有者注册
$wp_query (object) //Class_Reference/WP_Query 类实例
$wp_rewrite (object) //Class_Reference/WP_Rewrite 类实例
$wp (object) //Class_Reference/WP 类实例
$wpdb (object) //Class_Reference/wpdb 类实例
$wp_locale (object)  //本地化信息
$wp_admin_bar (WP_Admin_Bar)  //管理工具条对象
$wp_roles (WP_Roles) //WordPress 角色对象
$wp_meta_boxes (array) //已注册 metaboxes 的对象, 包含他们的 id, 参数, 回调函数、标题等信息
$wp_registered_sidebars (array) //已注册的小工具区域
$wp_registered_widgets (array) //已注册的小工具
$wp_registered_widget_controls (array) //已注册的小工具字段
$wp_registered_widget_updates (array) //已注册的小工具更新
后台全局变量
$pagenow (string) //在 wp-admin 中使用 同时参考 get_current_screen() 以了解 WordPress Admin Screen API
$post_type (string) //在 wp-admin 中使用 当前页面的文章类型
$allowedposttags (array) //允许使用的文章标签
$allowedtags (array) //允许使用的标签
$menu (array) //WordPress 的后台菜单数据