Passing and retrieving query vars in wordpress
-
16-10-2019 - |
문제
I have two authors pages, one displays about 5 posts. Then I'm trying to setup another page that will be all of their posts. I have created a template called moreauthorposts.php and I'm trying to pass the author variable to this page. Problem is if i pass domain.com/more-author-posts?author=johndoe it gets stripped out. How can I retrieve this value? Is this even possible in wordpress? I know WP Rewrite is jacking my URL structure somehow I'm just not sure.
I've tried:
get_query_var('author')
and tried reading this but didn't have any luck:
http://codex.wordpress.org/Query_Overview
Suggestions?
Thanks.
해결책
I'm almost sure that author
is built-in, so use something like author_more
. You will need to add that var to query_vars
first. Example:
// add `author_more` to query vars
add_filter( 'init', 'add_author_more_query_var' );
function add_author_more_query_var()
{
global $wp;
$wp->add_query_var( 'author_more' );
}
Then on your more-author-posts.php
template call it like this:
if ( get_query_var( 'author_more' ) )
{
// do your stuff
}
Update
This works in the following URl example/use case:
http://example.com/index.php?author_more=value
But if you want to use this as fancy URl, you need to add a rewrite rule:
add_action('init','add_author_more_rewrite_rule');
function add_author_more_rewrite_rule()
{
add_rewrite_rule(
'more-author-posts/(\d*)$',
'index.php?author_more=$matches[1]',
'top'
);
}
Now you can use it like this
http://example.com/more-author-posts/value