gpt4 book ai didi

wordpress - 将分页添加到页面中的自定义帖子循环

转载 作者:行者123 更新时间:2023-12-04 16:48:09 26 4
gpt4 key购买 nike

我创建了一个自定义页面模板(testimonials-page.php),在该模板中
使用以下循环加载自定义帖子类型“推荐”:

<?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(); ?>

如何在其中添加分页?我安装了WP Paging插件,并且该插件正常工作
当我使用以下命令将分页调用到category.php时,效果很好:
<p><?php wp_paging(); ?></p>

将相同的内容插入testimonial-page.php会导致格式和链接损坏
404在我身上。

最佳答案

首先,除非您打算修改默认的Wordpress Loop,否则切勿使用query_posts。

而是切换到WP Query

这是我为使用所有内置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).'">&laquo;</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">&lsaquo;</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)).'">&rsaquo;</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">&raquo;</a>
</div>
';
wp_reset_postdata();
endif;
?>

2018年1月编辑:

还可以考虑使用 paginate_links,因为它也内置于Wordpress中,并且具有更强大的选项和功能。

关于wordpress - 将分页添加到页面中的自定义帖子循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11638909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com