我正在使用add_rewrite_rule()
要创建自定义端点,请执行以下操作:
示例。com/author/username/my endpoint我需要在此页面上分页,因为我正在使用WP_User_Query
. 当我访问这些URL时,我看到了我应该看到的用户,因此一切似乎都正常工作:
示例。com/author/username/my endpoint/示例。com/author/username/my endpoint/page/2示例。com/author/username/my endpoint/page/3/etc等The problem: 当我在查看分页页面(我的端点/页面/2/和我的端点/页面/3/等)时,访问Chrome开发工具中的网络选项卡,它告诉我WordPress正在返回404。
以下是我使用的完整代码:
// Add query var.
add_filter( \'query_vars\', function( $vars ) {
$vars[] = \'my-endpoint\';
return $vars;
} );
// Add rewrite rules.
add_rewrite_rule(
\'^author/([^/]+)/my-endpoint/page/([0-9]+)/?$\',
\'index.php?author_name=$matches[1]&my-endpoint=1&paged=$matches[2]\',
\'top\'
);
add_rewrite_rule(
\'^author/([^/]+)/my-endpoint/?$\',
\'index.php?author_name=$matches[1]&my-endpoint=1\',
\'top\'
);
// Template display
add_filter( \'template_include\', function( $template ) {
global $wp_query;
if ( array_key_exists( \'my-endpoint\', $wp_query->query_vars ) ) {
return trailingslashit( get_template_directory() ) . \'my-endpoint.php\';
}
return $template;
} );
// The user query in my my-endpoint.php template.
$page = max( 1, get_query_var( \'paged\' ) );
$args = array(
\'count_total\' => true,
\'include\' => $ids,
\'number\' => get_option( \'posts_per_page\' ),
\'offset\' => ( $page - 1 ) * get_option( \'posts_per_page\' ),
\'order\' => \'DESC\',
\'orderby\' => \'include\',
\'paged\' => $page
);
$wp_user_query = new WP_User_Query( $args );
为什么WordPress在分页页面上返回404?
示例。com/author/username/my endpoint/page/2示例。com/author/username/my endpoint/page/3/etc等
Update
我尝试使用我自己的查询变量,如下面的答案所示,但我继续看到404:
// Add query var.
add_filter( \'query_vars\', function( $vars ) {
$vars[] = \'my-endpoint\';
$vars[] = \'userpaged\';
return $vars;
} );
// Add rewrite rules.
add_rewrite_rule(
\'^author/([^/]+)/my-endpoint/userpaged/([0-9]+)/?$\',
\'index.php?author_name=$matches[1]&my-endpoint=1&userpaged=$matches[2]\',
\'top\'
);