[英]Wordpress WP_Query with pagination
我一直在使用Google搜索,並查看了許多與此相關的其他問題,但問題仍然存在。
我正在嘗試獲取分頁查詢,僅顯示第1頁。 如果將分頁的變量更改為任何其他數字,它將返回一個空數組。
這是我使用的代碼,與我在其他問題中看到的代碼非常相似:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$temp = $wp_query;
$wp_query= null;
$args = array(
'posts_per_page' => 20,
'post_type' => 'post',
'paged' => $paged,
'category_name' => 'blog'
);
$wp_query = new WP_Query($args);
$data = array();
while ($wp_query->have_posts()) : $wp_query->the_post();
$obj = new stdClass;
$obj->id = $post->ID;
$obj->title = $post->post_title;
$obj->excerpt = substr(str_replace(array("\r","\t","\n"), array('','',''), trim(strip_tags($post->post_content))), 0, 200).'...';
$obj->slug = str_replace('http://localhost/', '', get_permalink($post->ID));
$obj->author_name = get_user_by('id', $post->post_author)->user_login;
$obj->featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'post-thumbnails') );
array_push($data, $obj);
endwhile;
怎么了? 我猜不到!
寫在你的functions.php
//Pagination
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 queries
* 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>";
}
}
並在您想顯示分頁的任何地方使用以下代碼
<?php
if (function_exists(custom_pagination)) {
custom_pagination($cat->max_num_pages,"",$paged);
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.