将重写终结点添加到作者页面+分页

时间:2013-09-06 作者:chowwy

我想向作者页面添加一个新端点,以便URL如下所示:

http://example.com/writer/username/articles

到目前为止,我已经:

更改了作者库添加了重写规则设置查询变量添加了端点使用模板重定向到使用我的模板结果:未找到404页(即使在刷新重写规则之后)。我想不出问题出在哪里。此外,我还想向端点添加分页。下面是我的代码:

更改了作者库:

function foo_change_author_base() {
    global $wp_rewrite;
    $wp_rewrite->author_base = \'writer\';
}
add_filter( \'init\', \'foo_change_author_base\', 0 );
添加了重写规则:

function foo_rewrite_rule()
{
    add_rewrite_rule( \'writer/([^/]+)/articles(/(.*))?/?$\', \'index.php?author_name=$matches[1]&articles=$matches[3]\', \'top\' );
}
add_action( \'init\', \'foo_rewrite_rule\' );
设置查询变量:

function foo_add_vars($vars) {
    $vars[] = \'articles\';   
    return $vars;
}
add_filter(\'query_vars\', \'foo_add_vars\');
添加了端点:

function foo_add_endpoint() {
    global $wp_rewrite;
    add_rewrite_endpoint( \'articles\', EP_AUTHORS | EP_PAGES );
}
add_action( \'init\', \'foo_add_endpoint\');
并添加了我的模板:

function foo_rewrite_template()
{
    global $wp_query;

    if ( array_key_exists( \'articles\', $wp_query->query_vars ) ) {
        include (get_template_directory_uri() . \'/articles.php\');
        exit;
    }
}
add_action( \'template_redirect\', \'foo_rewrite_template\' );

2 个回复
最合适的回答,由SO网友:cybmeta 整理而成

您的代码应该可以工作。线路:

include (get_template_directory_uri() . \'/articles.php\');
需要在服务器配置中设置allow\\u url\\u include=1,因为您正试图通过http包含文件。你能检查一下这个吗?

您还必须知道,template\\u redirect应该用于真正的重定向include() 这里可能有不希望的影响。我认为您真正想要的是template\\u包括:

add_filter(\'template_include\', \'my_template_include\');

function my_template_include( $template ) {
    global $wp->query;
    if ( array_key_exists( \'articles\', $wp_query->query_vars ) ) {
        return get_template_directory() . \'/articles.php\';
    }
   return $template;
}
下面是我的代码:

function foo_change_author_base() {
    global $wp_rewrite;
    $wp_rewrite->author_base = \'writer\';
}
add_filter( \'init\', \'foo_change_author_base\', 0 );

function foo_rewrite_rule()
{
    add_rewrite_rule( \'writer/([^/]+)/articles/page/([0-9]{1,})/?$\', \'index.php?author_name=$matches[1]&articles=1&paged=$matches[2]\', \'top\' );
    //If you don\'t want the last trailing slash change the las /?$ to just $
    add_rewrite_rule( \'writer/([^/]+)/articles/?$\', \'index.php?author_name=$matches[1]&articles=1\', \'top\' );
}
add_action( \'init\', \'foo_rewrite_rule\' );

function foo_add_vars($vars) {
    $vars[] = \'articles\';   
    return $vars;
}
add_filter(\'query_vars\', \'foo_add_vars\');

function foo_add_endpoint() {
    add_rewrite_endpoint( \'articles\', EP_AUTHORS );
}
//add_action( \'init\', \'foo_add_endpoint\'); // Commented out
// Edit: add_rewrite_endpoint is not needed when using add_rewrite_rule


// auto-add the articles endpoint to author links
function foo_author_link_filter( $link, $author_id, $author_nicename ) {
    return user_trailingslashit($link) . \'articles/\';
}
add_filter( \'author_link\', \'foo_author_link_filter\', 20, 3);

add_filter(\'template_include\', \'foo_template_include\');
function foo_template_include( $template ) {
    global $wp_query;
    //You may need some other checks
    if ( array_key_exists( \'articles\', $wp_query->query_vars ) ) {
        return get_template_directory() . \'/articles.php\';
    }
    return $template;
}
现在转到您的站点。com/作者/作者\\u姓名/文章/

SO网友:gmazzap

首先,如果添加端点,则不需要添加重写规则。但是,如果希望处理分页,最好使用重写规则。真的有两条重写规则,一条用于未分页。

在此之后,您的文章查询var不打算包含任何内容,只想“存在”或“不存在”。因此,如果存在,可以将其设置为1。

然后只需在模板include上添加一个过滤器,就像他的答案中的@cybnet suggest。

所有代码应类似于:

function foo_change_author_base() {
    global $wp_rewrite;
    $wp_rewrite->author_base = \'writer\';
}
add_filter( \'init\', \'foo_change_author_base\', 0 );

function foo_add_vars($vars) {
    $vars[] = \'articles\';   
    return $vars;
}
add_filter(\'query_vars\', \'foo_add_vars\');  

function foo_rewrite_rule() {
    add_rewrite_rule( \'writer/([^/]+)/articles/page/([^/]*)$\', \'index.php?author_name=$matches[1]&articles=1&paged=$matches[2]\', \'top\' );
    add_rewrite_rule( \'writer/([^/]+)/articles(.*)$\', \'index.php?author_name=$matches[1]&articles=1\', \'top\' );
}
add_action( \'init\', \'foo_rewrite_rule\' );

function foo_rewrite_template( $template ) {
    if ( ! is_author() ) return $template;
    global $wp_query;
    if ( $wp_query->get(\'articles\') ) return ( get_template_directory(). \'/articles.php\');
    return $template;
}
add_filter( \'template_include\', \'foo_rewrite_template\' );

// add a filter to author link, so \'get_author_posts_url\' return the customized url
function foo_author_link_filter( $link, $author_id, $author_nicename ) {
    return user_trailingslashit($link) . \'articles/\';
}
add_filter( \'author_link\', \'foo_author_link_filter\', 20, 3);
这对分页很有效。只记得再次保存永久链接设置。

在测试此代码的测试环境的打印屏幕下方:

enter image description here

结束

相关推荐

Author Nickname URL Friendly

使用以下代码更改作者页面url以使用“昵称”。工作正常,但当昵称是多个单词时,它不会使其url友好。有什么办法吗?add_filter( \'request\', \'wpse5742_request\' ); function wpse5742_request( $query_vars ) { if ( array_key_exists( \'author_name\', $query_vars ) ) { global $wpdb; $author_