PHP global 关键词
global 关键词用于访问函数内的全局变量。
要做到这一点,请在(函数内部)变量前面使用 global 关键词。wordpress 有关于查询的全局变量
- global $query_string; 查询的语句。
- global $wp_query; 关于查询的语句和结果的对象
- global paged;一些查询语句的字段,它单独于 wp_query。
tip:wordpress把查询的语句放在全局的字段和对象中,这样不利于集中管理。
比如:想手动改变 paged,改变 wp_query对象是不行的,必须改变$paged
/**
* Return the next posts page link.
*
* @since 2.7.0
*
* @global int $paged
* @global WP_Query $wp_query
*
* @param string $label Content for link text.
* @param int $max_page Optional. Max pages.
* @return string|void HTML-formatted next posts page link.
*/
function get_next_posts_link( $label = null, $max_page = 0 ) {
global $paged, $wp_query;
if ( !$max_page )
$max_page = $wp_query->max_num_pages;
if ( !$paged )
$paged = 1;
$nextpage = intval($paged) + 1;
if ( null === $label )
$label = __( 'Next Page »' );
if ( !is_single() && ( $nextpage <= $max_page ) ) {
这是wordpress中“下一页”,必须给 $paged 赋值才管用。