模板文件中的自定义URL参数

时间:2011-08-16 作者:Kevin Hoffman

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?

我在关闭所有插件的情况下进行了测试,并尝试了默认的永久链接,但得到了相同的结果。

3 个回复
SO网友:Bainternet

这个global $wp_query 当你单身的时候,对象并不是你想要的。php。而是使用global $wp 对象尝试将代码更改为:

 global $wp;
 if (array_key_exists(\'myvar\', $wp->query_vars) && isset($wp->query_vars[\'myvar\'])){
    echo \'custom variable recognized\';
 }
 print_r($wp->query_vars);

SO网友:JohnMetta

我选择了简单的方法,只是用了这样的方法:

$user_id = $_GET[\'user_id\'];
$user = new WP_User($user_id);
在模板中。然后我可以用如下URL调用此模板http://site.com/profile/?user_id=4 它在任何地方都有效。虽然不像过滤器那样“完全WordPress”,但它可以工作并且相当干净。

SO网友:Ron

我正在处理相同的问题,我认为我们还需要使用add\\u query\\u arg来嵌入?myvar=someValue,输入要调用的新url(比如从

$url_with_param = add_query_arg( \'myvar\', \'someValue\', site_url(\'/index.php/\') ); <a href=\'$url_with_param\'>Click Here</a>";

add\\u过滤器只是使用

$wp_query->query_vars[\'myvar\']

我知道这不是一个完整的解决方案,但这是一个重要的步骤。

结束

相关推荐

限制访问wp-login.php

在我的主题侧栏中有一个硬编码的登录/注册/密码丢失表单。我正在为“redirect\\u to”使用隐藏输入,它工作正常。但是,当我按下登录按钮而不输入任何内容时,或者当出现错误时,它会重定向到wp login。php页面。我不想发生这种事。如果发生错误,它应该显示在当前位置。我怎样才能做到这一点。注意:注销功能也需要相同的功能。基本上,我不想显示wp登录。php页面,任何人都可以不惜任何代价。