好吧,所以我知道这是一个愚蠢的标题,但这正是我现在的感受。。
我已经为WordPress创建了一些自定义重写,它们工作得很好,唯一的问题是现在我需要添加分页。我的规则会影响分页,反之亦然。当我单击分页链接时,页面url将更新,但查询保持不变。。
下面是我的函数。php
// ADD REWRITE FOR VIDEOS -> GROUP
add_rewrite_tag(\'%group%\',\'([^&]+)\');
// ADD REWRITE FOR VIDEOS -> VALUE
add_rewrite_tag(\'%value%\',\'([^&]+)\');
// ADD REWRITE FOR VIDEOS TO ACCEPT GROUP & VALUE
add_rewrite_rule(\'^car-videos/group/([^/]*)/value/([^/]*)/?\',\'index.php?page_id=40&group=$matches[1]&value=$matches[2]\',\'top\');
这是我的分页代码
<div class="pagination">
<?php $total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var(\'paged\'));
echo paginate_links(array(
\'base\' => get_pagenum_link(1) . \'%_%\',
\'format\' => \'page/%#%\',
\'current\' => $current_page,
\'total\' => $total_pages,
\'prev_text\' => __(\'Previous\'),
\'next_text\' => __(\'Next\')
));
}
?>
</div>
下面是如何获取组和值变量
$urlGroup = $wp_query->query_vars[\'group\'];
$urlValue = $wp_query->query_vars[\'value\'];
我在WP\\u查询中使用此选项
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
如何重写规则以允许分页?我做错什么了吗?
最合适的回答,由SO网友:cybmeta 整理而成
我不确定,但我认为您只需要添加两个查询变量,“group”和“value”,而不是两个重写标记。你能试试这个吗:
add_filter(\'query_vars\', \'cyb_add_query_vars\');
function cyb_add_query_vars( $vars) {
$vars[] = "group"; // name of the var as seen in the query string and URL
$vars[] = "value";
return $vars;
}
add_action(\'init\',\'cyb_add_rewrite_rules\');
function cyb_add_rewrite_rules() {
add_rewrite_rule( \'^car-videos/group/([^/]*)/value/([^/]*)/?$\',\'index.php?page_id=40&group=$matches[1]&value=$matches[2]\',\'top\');
//Rule for pagination
add_rewrite_rule( \'^car-videos/group/([^/]*)/value/([^/]*)/page/([0-9]{1,})/?$\', \'index.php?page_id=40&group=$matches[1]&value=$matches[2]&paged=$matches[3]\', \'top\' );
}
那么这应该适用于分页:
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
您可以通过以下方式获得“组”和“值”的值:
$group = get_query_var(\'group\');
$value = get_query_var(\'value\');
//OR
global $wp_query;
$group = $wp_query->get( \'group\' );
$value = $wp_query->get(\'value\');