我有两个作者页面,一个显示大约5个帖子。然后,我试图设置另一页,这将是他们的所有帖子。我创建了一个名为MoreAuthorPosts.php的模板,并且正在尝试将作者变量传递给此页面。问题是,如果我通过domain.com/more-author-posts?如何检索这个值?在WordPress中这甚至可以吗?我知道WP重写正在以某种方式引起我的URL结构。

我试过了:

get_query_var('author')

并尝试阅读此书,但没有任何运气:

http://codex.wordpress.org/query_overview

建议?

谢谢。

有帮助吗?

解决方案

我几乎确定 author 是内置的,所以请使用 author_more. 。您需要将该var添加到 query_vars 第一的。例子:

// 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' );
}

然后在你的 more-author-posts.php 模板这样称呼:

if ( get_query_var( 'author_more' ) )
{
    // do your stuff
}

更新

这在以下URL示例/用例中起作用:

http://example.com/index.php?author_more=value

但是,如果您想将其用作花哨的URL,则需要添加重写规则:

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'
    );
}

现在您可以这样使用

http://example.com/more-author-posts/value
许可以下: CC-BY-SA归因
scroll top