首页 > 解决方案 > Get the var from url

问题描述

I have url like this:

index.php?category_name=$matches[1]/$matches[2]&var1=yes&paged=$matches[3]

and I want to get var1 from this url, how to do it?

Regards

标签: wordpress

解决方案


在全局 $wp_query 对象的 WP_Query 类中检索公共查询变量。

用法

<?php get_query_var( $var, $default ) ?>

参数

$var 要检索的变量键。

例子

// Test if the query exists at the URL
if ( get_query_var('var1') ) {

    // If so echo the value
    echo get_query_var('var1');

}

获取 URL 变量

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['var1'];

推荐阅读