我正在尝试使用自定义主题进行分页。
我有一个带有自定义分类的帖子类型,我对这些自定义分类法进行了分页,并且工作正常。
然而,在网站的另一部分,我有另一种自定义帖子类型;我试图通过这个帖子类型的帖子分页。目前,帖子也在此帖子类型的自定义分类中,自定义分类法的行为类似于一个类别。
网址结构为http://www.domain.com/latest-news/,但是当我转到http://www.domain.com/latest-news/2/时出现404错误,这就是问题所在。
这是我的代码(对不起,这么久):
<?php
$numCats = 0;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$posts_per_page = 5;
$offset = ($posts_per_page * $paged - $posts_per_page);
$taxonomies = array(
'latest-cat'
);
$args = array(
'hide_empty' => false,
'parent' => 0,
'orderby' => 'id',
'order' => 'DESC',
'paged' => $paged
);
$terms = get_terms($taxonomies, $args);
foreach ($terms as $term) {
$args = array(
'post_type' => 'latest_news_item',
'oserby' => 'id',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'latest-cat',
'terms' => $term->term_id
)
)
);
$wp_query = new WP_Query($args);
while ($wp_query->have_posts() ) : $wp_query->the_post();
$numCats++;
endwhile;
$args = array(
'post_type' => 'latest_news_item',
'parent' => 115,
'orderby' => 'id',
'order' => 'ASC',
'posts_per_page' => $posts_per_page,
'offset' => 0,
'paged' => $paged
);
$wp_query = new WP_Query($args);
while ($wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="news-events-item">
<h3><?php echo get_the_title(); ?></h3>
<?php $content = get_the_content(); ?>
<p class="summary"><?php echo wp_trim_words($content, '30', '...'); ?><a href="<?php echo get_the_permalink(); ?>" class="read-more">More</a></p>
</div>
<?php
endwhile;
} ?>
$ numCats变量只是计算帖子的数量,以便我可以设置分页链接,这部分可以正常工作。
谢谢
答案 0 :(得分:0)
我以这种方式为自己解决了这个问题:
在自定义帖子类型“博客”的存档页面上。
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=blog&paged=' . $paged );
$args = array(
'post_type' => 'blog',
'paged' => $paged,
'orderby' => 'date',
'order' => 'DEST',
'posts_per_page' => '1',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
这是经典循环,为帖子类型“博客”添加了参数并添加了“分页”属性。 显然这些要点:
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=blog&paged=' . $paged );
最后,循环之外是functions.php的分页输出
<?php echo pagination($loop->max_num_pages); ?>
functions.php里面是permalink修改,也是把posts_per_page设置为ONE post的修改,可以测试分页。
function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => @add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'plain'
);
if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
}
function portfolio_posts_per_page( $query ) {
if ( $query->query_vars['post_type'] == 'blog' ) $query->query_vars['posts_per_page'] = 1;
return $query;
}
if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_posts_per_page' );
And as last here is the pagination code which is used on the archive page outside of the loop.
**used with bootstrap 5.0**
function pagination($pages = '', $range = 1)
{
$showitems = ($range * 2) + 1;
global $paged;
if (empty($paged)) $paged = 1;
if ($pages == '') {
global $the_query;
$pages = $the_query->max_num_pages;
if (!$pages) {
$pages = 1;
}
}
if (1 != $pages) {
echo "<nav aria-label=\"...\">
<ul class=\"pagination\">";
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link(1) . "\">První</a>
</li>";
if ($paged > 1 && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link($paged - 1) . "\">Předchozí</a>
</li>";
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
echo ($paged == $i) ? "<li class=\"page-item active\"><a class=\"page-link\" href=\"" . get_pagenum_link($i) . "\">" . $i . "</a></li>" : "<li class=\"page-item\"><a class=\"page-link\" href=\"" . get_pagenum_link($i) . "\">" . $i . "</a></li>";
}
}
if ($paged < $pages && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link($paged + 1) . "\">Další</a>
</li>";
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link($pages) . "\">Poslední</a>
</li>";
echo "</ul>";
echo "</div>";
//echo "<div class='all_pages text-muted'><small>Stránek " . $pages . "</small></div>";
}
}
我希望它有帮助,它对我有用。
(抱歉格式化,堆栈溢出编辑器太牛了...)