[英]Pagination at wordpress site is working with plain permalinks (com/?p=123) but not with custom (/%postname%/)
我正在嘗試在wordpress中創建我的第一個網站,但我面臨分頁問題。 我花了兩天的時間弄清楚問題出在哪里,但我不能...當我將永久鏈接設置為純文本並且站點的重定向(例如/?page_id = 6032&paged = 2)時,分頁正常。 但是,當我將永久鏈接設置為自定義(重定向將我發送到/ accommodation / page / 2 /)時,將我引導至未找到的頁面。 這幾天我進行了很多搜索,發現了很多解決方案,但似乎沒有任何效果。 奇怪的是,我對網站的另一頁使用了完全相同的分頁,因此它可以正常工作。 分頁不起作用的住宿頁面是這樣的
<?php
$accommodation_perpage = '6';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( "post_type=accommodation&posts_per_page=$accommodation_perpage&paged=$paged" );
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
和正在工作的推薦頁面相同,只是有所不同
query_posts( "post_type=testimonial&posts_per_page=$testimonial_perpage&paged=$paged" )
我的分頁代碼是
function pagination() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/ ** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
請將此代碼添加到您的function.php文件中
function custom_numeric_pagination() {
if (is_singular())
return;
global $wp_query;
if ($wp_query->max_num_pages <= 1)
return;
$paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
$max = intval($wp_query->max_num_pages);
if ($paged >= 1)
$links[] = $paged;
if ($paged >= 3) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if (( $paged + 2 ) <= $max) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
if (!in_array(1, $links)) {
$class = 1 == $paged ? ' class="active"' : '';
printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link(1)), '1');
if (!in_array(2, $links))
echo '<li>…</li>';
}
sort($links);
foreach ((array) $links as $link) {
$class = $paged == $link ? ' class="active"' : '';
printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($link)), $link);
}
if (!in_array($max, $links)) {
if (!in_array($max - 1, $links))
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($max)), $max);
}
echo '</ul></div>' . "\n";
}
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'yourcustomposttypename', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.