我试图在自定义模板中对自定义帖子类型进行分页。我试着用这种方式:
<?php
// The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array( 'post_type' => 'portfolio', 'portfolio_category' => 'projektowanie', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page'=>3, 'paged'=>$paged ) );
$i = 1;
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
?>
<a href="#" data-toggle="modal" data-target="#pro<?php echo $i;?>" class="col-md-4 item-portfolio">
<img src="<?php echo $thumbnail[0]; ?>" alt="">
<h4><?php the_title(); ?></h4>
<p><?php echo get_post_meta( $post->ID, 'subtitle', true ); ?></p>
</a>
<?php if($i % 3 == 0){ ?>
<div class="clearfix"></div>
<div style="margin-top: 0px; "class="shadow"></div>
<?php } $i++; endwhile; ?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $the_query->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $the_query->max_num_pages) ?></li>
</ul>
当我点击下一步时,它会转到/ portfolio / page / 2 /但它会加载index.php而不是portfolio.php文件,并且不会显示自定义帖子类型的下一项。我在哪里弄错了?
答案 0 :(得分:0)
而不是这个
<ul>
<li><?php previous_posts_link( '« PREV', $the_query->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $the_query->max_num_pages) ?></li>
</ul>
使用此
<div class="pagination">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</div>
答案 1 :(得分:0)
嘿,在function.php文件中添加此函数
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
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) . '%_%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«' , 'swift'),
'next_text' => __('»' , 'swift'),
'type' => 'array',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
echo '<div class="Pagination-Num"><ul>';
foreach ( $paginate_links as $paginate_link ) {
echo "<li> $paginate_link </li>";
}
echo '</ul></div>';
}
然后在您的自定义模板文件中添加以下代码段
<?php
// The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array( 'post_type' => 'portfolio', 'portfolio_category' => 'projektowanie', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page'=>3, 'paged'=>$paged ) );
$i = 1;
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
?>
<a href="#" data-toggle="modal" data-target="#pro<?php echo $i;?>" class="col-md-4 item-portfolio">
<img src="<?php echo $thumbnail[0]; ?>" alt="">
<h4><?php the_title(); ?></h4>
<p><?php echo get_post_meta( $post->ID, 'subtitle', true ); ?></p>
</a>
<?php if($i % 3 == 0){ ?>
<div class="clearfix"></div>
<div style="margin-top: 0px; "class="shadow"></div>
<?php } $i++; endwhile; ?>
<nav>
<?php custom_pagination($the_query->max_num_pages,"",$paged);?>
<?php
if ($the_query->max_num_pages > 1) {
?>
<ul>
<li><?php echo get_next_posts_link( 'NEXT »', $the_query->max_num_pages );?> </li>
<li><?php echo get_previous_posts_link( '« PREV' ); ?></li>
</ul>
我认为它可以帮助你