SETUP
我想从URL中提取一个查询变量,并在我的单个post模板(single.php)上使用它。例如:mysite。com/2011/07/29/testPost/?myvar=someValue
为此,我向函数中添加了自定义查询变量。php类似:
add_filter(\'query_vars\', \'parameter_queryvars\' );
function parameter_queryvars( $qvars ) {
$qvars[] = \'myvar\';
return $qvars;
}
然后是单曲。php和索引。php我添加了以下代码来测试是否设置了自定义变量,如果为true,则返回字符串。我还打印了$wp\\u query->query\\u vars的内容,以便查看我的自定义变量是否存在:
<?php
global $wp_query;
if( isset( $wp_query->query_vars[\'myvar\'] )) {
echo \'custom variable recognized\';
}
print_r($wp_query->query_vars);
?>
PROBLEM
当我访问像mysite这样的URL时。com/2011/07/29/testPost/?myvar=someValue,它使用单个。php模板中,不会显示echo命令,print\\u r()的输出显示query\\u vars列表中没有我的“myvar”。
从单个输出。php:
Array ( [p] => 0 [page] => 0 [year] => 2011 [monthnum] => 7 [day] => 29 [name] => testpost [error] => [m] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [post_type] => [posts_per_page] => 150 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC [orderby] => wp_posts.post_date DESC )
然而,当我访问像mysite这样的URL时。com/?myvar=someValue,它使用索引。php模板,echo命令输出,您可以看到print\\u r()输出的第一个变量是我要查找的“myvar”:
Array ( [myvar] => someValue [error] => [m] => 0 [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [meta_query] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [post_type] => [posts_per_page] => 150 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC [orderby] => wp_posts.post_date DESC )
QUESTION
为什么要在索引上正确设置自定义查询变量。php页面,但不在单个页面上。php?
我在关闭所有插件的情况下进行了测试,并尝试了默认的永久链接,但得到了相同的结果。