Cyn*_*hia 5 wordpress pagination custom-post-type
我创建了一个自定义页面模板(testimonials-page.php),在该模板中,我使用以下循环加载自定义帖子类型'testimonials':
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Run Code Online (Sandbox Code Playgroud)
如何为此添加分页?我安装了WP Paging插件,当我使用以下命令将分页调用category.php时,该插件工作得很好:
<p><?php wp_paging(); ?></p>
Run Code Online (Sandbox Code Playgroud)
在testimonial-page.php中插入相同的内容会导致格式化,并且404链接在我身上.
Haaaaalp!
谢谢 :)
mai*_*o84 13
首先,除非您打算修改默认的Wordpress循环,否则永远不要使用query_posts.
而是切换到WP查询.
这是我为使用所有内置Wordpress函数的客户端所做的主题编写的内容.到目前为止,它对我来说一直很好用,所以我会尽可能地将它集成到你的代码中:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Run Code Online (Sandbox Code Playgroud)
还要考虑使用paginate_links,因为它也内置于Wordpress中,并且具有更强大的选项和功能.