WordPress将分页添加到自定义帖子类型

时间:2014-10-27 10:42:55

标签: php wordpress pagination wp-query

在WordPress中,我目前正在使用以下代码显示testimonials页面的内容和自定义帖子类型testimonials中的帖子。

我试图添加分页但无效,没有出现任何错误,它们只是不显示。我猜这可能与我使用两个循环而不得不重置的事实有关,但我不确定?

需要更多信息,请询问。

<?php /* Template Name: Testimonials */ get_header(); ?>

<div id="main">

    <div class="fullwidth">

        <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

        <h1><?php the_field('h1_title'); ?></h1>

        <div class="width">

            <ul class="menu slider">

                <?php if(wp_list_pages('child_of='.$post->ID.'&echo=0&exclude=2')) { $list_of_pages = wp_list_pages('echo=0&title_li=&depth=1&child_of='.$post->ID.'&exclude=2'); $list_pages = explode('<li class="',$list_of_pages); $len = count($list_pages); for ($i = 0; $i <= $len-2; $i++) : echo $list_pages[$i] . '<li  class="list-item-' . ($i+1) . ' '; endfor; echo $list_pages[$i]; }

                elseif(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) { $list_of_pages = wp_list_pages('echo=0&title_li=&depth=1&child_of='.$post->post_parent.'&title_li=&exclude=2'); $list_pages = explode('<li class="',$list_of_pages); $len = count($list_pages); for ($i = 0; $i <= $len-2; $i++) : echo $list_pages[$i] . '<li  class="list-item-' . ($i+1) . ' '; endfor; echo $list_pages[$i]; } ?>

            </ul>

        </div>

        <h2><?php the_field('h2_title'); ?></h2>

        <?php the_content(); ?>

        <?php endwhile; wp_reset_postdata(); ?>

        <div class="posts">

            <?php $query = new WP_Query( array ( 'post_type' => 'testimonial', 'order' => 'DESC', 'posts_per_page' => 9, 'orderby'=> menu_order ) ); while ( $query->have_posts() ) : $query->the_post(); ?>

            <div class="blog-post">

                <div class="content">

                    <h3><?php the_title();?></h3>

                    <h4><?php the_field('visit_date'); ?></h4>

                    <?php the_content(); ?>

                </div>

                <div class="clear"></div>
            </div>

            <?php endwhile; ?>

            <div class="navigation">

                <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>

                <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>

            </div>

        </div>

    </div>

    <?php get_footer(); ?>

1 个答案:

答案 0 :(得分:1)

我觉得你没有收到并传递页面#,尝试更改/替换你的代码

$query = new WP_Query( array ( 'post_type' => 'testimonial', 'order' => 'DESC', 'posts_per_page' => 9, 'orderby'=> menu_order ) ); while ( $query->have_posts() ) : $query->the_post();

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;

$query = new WP_Query( array ( 'post_type' => 'testimonial', 'order' => 'DESC', 'posts_per_page' => 9, 'orderby'=> 'menu_order','paged' => $page ) ); 
while ( $query->have_posts() ) : $query->the_post();