好吧,我想我找到了一些真正有用的东西,我正在发布它,以防有人需要它。
解决方案只是避免仅为此页面使用Pagenavi,使用paginate_links()
取而代之的是,迫使它或多或少地表现得像Pagenavi,以保持整个网站的一致性。
让我解释一下。
我的自定义查询现在如下所示:
$query = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE
$wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = \'publish\'
AND $wpdb->posts.post_type = \'post\'
AND $wpdb->posts.post_date <= NOW()
AND (
$wpdb->posts.post_author = $curauth->ID
OR (
$wpdb->postmeta.meta_key = \'co_author\'
AND $wpdb->postmeta.meta_value = $curauth->ID
)
)
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date DESC
";
然后,我为分页创建了一些有用的变量:
global $wpdb;
global $post;
$total_query = "SELECT COUNT(*) FROM ($query) AS combined_table";
// Total count of rows
$total = $wpdb->get_var($total_query);
// Number of rows per page, as set in Wordpress settings
$items_per_page = get_option(\'posts_per_page\');
// Current page; defaults to 1 if not requested
$page = isset($_GET[\'page\']) ? abs((int) $_GET[\'page\']) : 1;
// Offset for query LIMIT
$offset = ($page*$items_per_page) - $items_per_page;
// Well... the next one is pretty much self-explained...
$last_page = ceil($total/$items_per_page);
然后执行查询并构建循环:
$posts = $wpdb->get_results($query." LIMIT $offset, $items_per_page");
foreach ($posts as $post) :
setup_postdata($post);
// The Loop
endforeach;
最后,分页(请注意,我使用的是
paginate_links()
/
wp_pagenavi()
要设置样式的类):
// Echo "Page # of #"
echo \'<span class="pages">Page \'.$page.\' of \'.$last_page.\'</span>\';
// If this isn\'t the first page...
if ($page != 1)
echo \'<a class="page-numbers" href="\'.$base_url.\'">< FIRST</a>\';
// I don\'t really know how to explain this one because I found it somewhere I can\'t find again, but it works!
echo paginate_links(
\'base\' => add_query_arg(\'page\', \'%#%\'),
\'prev_text\' => \'<\',
\'next_text\' => \'>\',
\'total\' => $last_page
\'current\' => $page
);
// If this isn\'t the last page...
if ($page != $last_page)
echo \'<a class="page-numbers" href="\'.$base_url.\'?page=\'.$last_page.\'">LAST ></a>\';
正如你可能已经注意到的,有一个
$base_url
变量:这是我唯一的警告。首先,这是
$base_url
:
$current_url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$current_url = explode(\'?\', $current_url);
$base_url = \'http://\'.$current_url[0];
警告是我不能
paginate_links()
要回显重写的URL,既不知道如何为第一页和最后一页链接生成URL。所以现在这个归档文件中的URL类似于
http://www.example.com/author/username/?page=2
而不是
http://www.example.com/author/username/page/2/
就是这样。我希望有人会觉得这很有用!