[英]Add pagination is custom archives page on WordPress (Thesis Theme)
我在頁面上使用以下 php 代碼來顯示來自單個類別的每頁 10 個帖子。
function my_page_of_posts5() {
if (is_page('10')) {
$custom_loop = new WP_Query('posts_per_page=10&cat=9');
echo '<div class="my-archives"><ul class="archive-list">';
if ( $custom_loop->have_posts() ) :
while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
echo '<li><a class="archive-link" href="'
. get_permalink() . '">'
. get_the_title()
. '</a> <span class="my-comment-count">( ';
comments_number('0', '1', '%');
echo ' )</span></li>';
the_excerpt();
endwhile;
wp_reset_query();
endif;
echo '</ul></div>';
}
}
add_action('thesis_hook_after_content','my_page_of_posts5');
我正在使用 Thesis WordPress 主題框架。
我想在頁面上的這些帖子之后顯示分頁。
我應該在那里放什么代碼來顯示分頁?
我試過使用 WP-Paginate 插件並放置 wp-paginate(); 頁面上的功能,但它不能正常工作。
需要另一種方式來解決它。
在不了解更多有關嘗試使用wp_paginate
時查詢的確切行為是什么的wp_paginate
,我建議的最好方法是添加'paged=' . get_query_var( 'paged' )
wp_paginate
'paged=' . get_query_var( 'paged' )
'paged=' . get_query_var( 'paged' )
到您的WP_Query
args,comme ça:
$custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . get_query_var( 'paged' ));
添加后, wp_paginate 可能適合您。
試一試!
更新
好的,從一開始:
paged
查詢變量posts_nav_link
獲取必要的導航你的整個片段應該像這樣工作:
function my_page_of_posts5() {
if (is_page('10')) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // what page? if none, one
$custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . $paged);
echo '<div class="my-archives"><ul class="archive-list">';
if ( $custom_loop->have_posts() ) : while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
echo '<li><a class="archive-link" href="' . get_permalink() . '">' . get_the_title() . '</a> <span class="my-comment-count">( ';
comments_number('0', '1', '%');
echo ' )</span></li>';
the_excerpt();
endwhile;
wp_reset_query();
endif;
echo '</ul></div>';
posts_nav_link(); // your next and previous links
}
}
add_action('thesis_hook_after_content','my_page_of_posts5');
嘗試使用此代碼,替換您現在擁有的代碼。
它添加了我一直使用的分頁功能,不需要額外的插件。 易於定制。
<?php
function my_page_of_posts5() {
if (is_page('10')) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // what page? if none, one
$custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . $paged);
echo '<div class="my-archives"><ul class="archive-list">';
if ( $custom_loop->have_posts() ) : while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
echo '<li><a class="archive-link" href="' . get_permalink() . '">' . get_the_title() . '</a> <span class="my-comment-count">( ';
comments_number('0', '1', '%');
echo ' )</span></li>';
the_excerpt();
endwhile;
wp_reset_query();
endif;
echo '</ul></div>';
theme_pagination();
}
}
add_action('thesis_hook_after_content','my_page_of_posts5');
// based on http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin
function theme_pagination($pages = '', $range = 2) {
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages) {
$pages = 1;
}
}
if(1 != $pages) {
echo '<div class="pagination">';
//if($paged > 1 && $showitems < $pages)
echo '<a href="'.get_pagenum_link($paged - 1).'" class="previous" >';
echo _x('previous', 'paginate', THEME_L10n);
echo '<span>previouw-arrow</span></a>';
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
if ($paged == $i){
echo '<span class="current number">'.$i.'</span>';
} else {
echo '<a href="'.get_pagenum_link($i).'" class="inactive number" >'.$i.'</a>';
}
}
}
//if ($paged < $pages && $showitems < $pages)
echo '<a href="'.get_pagenum_link($paged + 1).'" class="next" ><span>next-arrow</span>';
echo _x('next', 'paginate', THEME_L10n);
echo '</a>';
echo "</div>\n";
}
}
您可以嘗試在示例中的代碼末尾添加此分頁代碼:
//Set Blog Reading Settings to 10!
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array( 'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'end_size'=> 1,
'mid_size'=> 5 ) );
該代碼適用於普通免費主題的存檔和類別頁面,但您使用的不是免費的,也不是開源的,因此很難預測結果。
我認為應該刪除帖子數量:
WP_Query('posts_per_page=10&cat=9')
// Just leave it like this:
WP_Query('cat=9')
原因是 WP 中每頁的帖子數是在閱讀設置中配置的。 在查詢中放置一個數量可能會阻止分頁。 不用說,分頁必須有 10 個以上的帖子才能工作。
祝你好運!
有兩種可能的解決方案:
(一世)。 解決方案#1:
我希望你會認為wp-pagenavi是這里最好的解決方案。 將它集成到 Thesis 主題中太容易了。 作為論文主題用戶,您已經在使用論文openhook插件。
現在只需按照步驟在論文主題中添加pagenavi 即可。
<?php cr_pagenavi(); ?>
(二)。 解決方案#2:
1.首先,我安裝了一個插件,它是Simple Pagination (而且它的使用非常簡單)。 如果您正在使用論文,請復制並粘貼此代碼到 thesis_hook_after_content 並點擊保存。
<div class="pagination">
<?php wp_simple_pagination(); ?>
</div>
2.轉到您的 custom_functions.php 並粘貼此代碼以刪除上一篇和下一篇文章
function no_home_post_nav() {
if (is_home())
remove_action('thesis_hook_after_content', 'thesis_post_navigation');
}
add_action('thesis_hook_before_content','no_home_post_nav');
3.在您的主頁上查看您的新分頁並將其自定義為您想要的任何樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.