我有两个使用WP\\u查询的循环。
第一个循环显示X个帖子。无分页
(loop A)
第二个循环显示Y柱。使用自定义分页
(loop B)
这有点管用;但是您可以看到第二个循环重复第一个循环中的X个帖子。首先想到的是设置一个偏移量(等于X),但它会破坏分页,而且Codex上有一整页专门讨论这个问题。它还有一个变通方法,但问题是我有两个循环,所以这个变通方法适用于这两个循环。
So the question is
我如何使这项工作按预期进行<一个可能的解决方案是使Codex中的代码成为第二个循环的“目标”,但我不是程序员,我不知道如何做到这一点。也许有更好的方法来实现我的目标,但我想不出任何其他的选择。
其他信息1)我不能只使用一个循环,因为我的标记有多么复杂
2)所有代码都是在网上找到的
3)两个循环都不受作者、类别或其他限制。它始终是相同的内容,两个不同的布局“组合”成一个“提要”。
and since I can\'t post more than 2 links, here is the pagination code
function custom_pagination($numpages = \'\', $pagerange = \'\', $paged=\'\') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
This first part of our function is a fallback
for custom pagination inside a regular loop that
uses the global $paged and global $wp_query variables.
It\'s good because we can now override default pagination
in our theme, and use this function in default quries
and custom queries.
**/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == \'\') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
We construct the pagination arguments to enter into our paginate_links
function.
**/
$pagination_args = array(
\'base\' => get_pagenum_link(1) . \'%_%\',
\'format\' => \'page/%#%\',
\'total\' => $numpages,
\'current\' => $paged,
\'show_all\' => False,
\'end_size\' => 1,
\'mid_size\' => $pagerange,
\'prev_next\' => True,
\'prev_text\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'type\' => \'plain\',
\'add_args\' => false,
\'add_fragment\' => \'\'
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class=\'custom-pagination\'>";
echo "<span class=\'page-numbers page-num\'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}
solution from Codex
add_action(\'pre_get_posts\', \'myprefix_query_offset\', 1 );
function myprefix_query_offset(&$query) {
//Before anything else, make sure this is the right query...
if ( ! $query->is_home() ) {
return;
}
//First, define your desired offset...
$offset = 1;
//Next, determine how many posts per page you want (we\'ll use WordPress\'s settings)
$ppp = get_option(\'posts_per_page\');
//Next, detect and handle pagination...
if ( $query->is_paged ) {
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($query->query_vars[\'paged\']-1) * $ppp );
//Apply adjust page offset
$query->set(\'offset\', $page_offset );
}
else {
//This is the first page. Just use the offset...
$query->set(\'offset\',$offset);
}
}
add_filter(\'found_posts\', \'myprefix_adjust_offset_pagination\', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 1;
//Ensure we\'re modifying the right query object...
if ( $query->is_home() ) {
//Reduce WordPress\'s found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}