好的,我最后到了那里。我无法使用WP_Query
类,因为我确实需要拥有自己的非常大和复杂的SQL。以下是我最终得到的:
在里面functions.php
我有自己的自定义SQL和逻辑,用于计算WP分页逻辑所需的值:
function vacancies_current( ){
global $wpdb, $paged, $max_num_pages, $current_date;
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$post_per_page = intval(get_query_var(\'posts_per_page\'));
$offset = ($paged - 1)*$post_per_page;
/* Custom sql here. I left out the important bits and deleted the body
as it will be specific when you have your own. */
$sql = "
SELECT SQL_CALC_FOUND_ROWS {$wpdb->posts}.*
FROM {$wpdb->posts}
....
GROUP BY {$wpdb->posts}.ID
ORDER BY {$wpdb->posts}.post_date DESC
LIMIT ".$offset.", ".$post_per_page."; ";
$sql_result = $wpdb->get_results( $sql, OBJECT);
/* Determine the total of results found to calculate the max_num_pages
for next_posts_link navigation */
$sql_posts_total = $wpdb->get_var( "SELECT FOUND_ROWS();" );
$max_num_pages = ceil($sql_posts_total / $post_per_page);
return $sql_result;
}
然后在我的模板文件中,我有:
<?php
$vacancies_current = vacancies_current();
/*followed by a standart loop to display your results */
?>
<div class="navigation">
<div class="previous panel"><?php previous_posts_link(\'« previous vacancies\',$max_num_pages) ?></div>
<div class="next panel"><?php next_posts_link(\'more vacancies »\',$max_num_pages) ?></div>
</div>
诀窍在于供应
previous_posts_link()
和
next_posts_link
这个
$max_num_pages
显然,在正确计算它时。
这很有效。希望对某人有所帮助:)
达莎