Wordpress 向页面中的自定义post循环添加分页

Wordpress 向页面中的自定义post循环添加分页,wordpress,pagination,custom-post-type,Wordpress,Pagination,Custom Post Type,我已经创建了一个自定义页面模板(推荐页面.php),在该模板中 使用以下循环加载自定义帖子类型“推荐信”: <?php query_posts(array( 'posts_per_page' => 5, 'post_type' => 'testimonials', 'orderby' => 'post_date', 'paged' => $paged ) ); ?> <?php if ( have_posts() ) while

我已经创建了一个自定义页面模板(推荐页面.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(); ?>


首先,除非你想修改默认的Wordpress循环,否则永远不要使用查询帖子

相反,切换到

下面是我为一个主题写的东西,我为一个使用所有内置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;
?>
global$paged;
$curpage=$paged$页码:1;
$args=数组(
“post_type”=>“推荐信”,
'orderby'=>'post_date',
“每页帖子数”=>5,
“paged”=>paged美元
);
$query=新的WP\u查询($args);
如果($query->have_posts()):而($query->have_posts()):$query->the_post();
?>
2018年1月编辑:

也考虑使用,因为它也内置到WordPress中,并且具有更健壮的选项和能力。

< P>尝试用分页自定义代码:

<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$custom_query_args = array(
    'post_type' => 'post', 
    'posts_per_page' => get_option('posts_per_page'),
    'paged' => $paged,
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
    //'category_name' => 'custom-cat',
    'order' => 'DESC', // 'ASC'
    'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );

if ( $custom_query->have_posts() ) :
    while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

        <article <?php post_class(); ?>>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
            <div><?php the_excerpt(); ?></div>
        </article>

    <?php
    endwhile;
    ?>

    <?php if ($custom_query->max_num_pages > 1) : // custom pagination  ?>
        <?php
        $orig_query = $wp_query; // fix for pagination to work
        $wp_query = $custom_query;
        ?>
        <nav class="prev-next-posts">
            <div class="prev-posts-link">
                <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
            </div>
            <div class="next-posts-link">
                <?php echo get_previous_posts_link( 'Newer Entries' ); ?>
            </div>
        </nav>
        <?php
        $wp_query = $orig_query; // fix for pagination to work
        ?>
    <?php endif; ?>

<?php
    wp_reset_postdata(); // reset the query 
else:
    echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>

通过
资料来源:


我试试看。出于好奇,为什么从来没有强调过query_posts的用法?我已经实现了它,虽然它确实显示了分页链接,但链接仍然是404。查看一些关于的有用信息。基本上,您是在修改每个查询的默认循环,而不是与查询对象本身交互。此外,由于您的重写(很可能是在.htaccess文件中),链接停止工作。此链接有效:备份.htaccess文件,将其从web服务器中删除,然后尝试在“设置”->“永久链接”下设置“永久链接”。希望这会把一切都弄清楚(前提是你有最新版本的Wordpress)
<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$custom_query_args = array(
    'post_type' => 'post', 
    'posts_per_page' => get_option('posts_per_page'),
    'paged' => $paged,
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
    //'category_name' => 'custom-cat',
    'order' => 'DESC', // 'ASC'
    'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );

if ( $custom_query->have_posts() ) :
    while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

        <article <?php post_class(); ?>>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
            <div><?php the_excerpt(); ?></div>
        </article>

    <?php
    endwhile;
    ?>

    <?php if ($custom_query->max_num_pages > 1) : // custom pagination  ?>
        <?php
        $orig_query = $wp_query; // fix for pagination to work
        $wp_query = $custom_query;
        ?>
        <nav class="prev-next-posts">
            <div class="prev-posts-link">
                <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
            </div>
            <div class="next-posts-link">
                <?php echo get_previous_posts_link( 'Newer Entries' ); ?>
            </div>
        </nav>
        <?php
        $wp_query = $orig_query; // fix for pagination to work
        ?>
    <?php endif; ?>

<?php
    wp_reset_postdata(); // reset the query 
else:
    echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>