WordPress - 自定义帖子类型分页

时间:2014-04-08 11:45:33

标签: wordpress pagination custom-post-type

我有一个与自定义分类(支持)相关的自定义帖子类型(问题)

在my-theme / taxonomy.php中我有这段代码:

<?php

$taxonomy = get_queried_object()->taxonomy;

if ($taxonomy == 'support')
{
    get_template_part('template/support/categories');
    exit;
}

wp_safe_redirect(site_url('/'));
exit;

?>

意味着我将特定模板文件作为“支持”分类标准。

在我的分类模板文件中,我制作了一个自定义查询:

<?php $current_category = get_term_by('id', get_queried_object()->term_id, 'support'); ?>

<?php $questions = new WP_Query(array(
    'post_type' => array('question'),
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'posts_per_archive_page' => 5,
    'paged' => ((get_query_var('page')) ? get_query_var('page') : 1),
    'nopaging' => false,
    'tax_query' => array(
        array(
            'taxonomy' => 'support',
            'terms' => array($current_category->term_id)
        )
    ),
    'orderby' => 'menu_order',
    'order' => 'ASC'
)); ?>

<?php if ($questions->have_posts()): ?>
    <ul class="list normalize">
        <?php while ($questions->have_posts()) : $questions->the_post(); ?>
            <li>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <?php the_excerpt(); ?>
            </li>
        <?php endwhile; ?>
    </ul>
<?php endif; ?>

<div class="nav-previous">
    <?php next_posts_link( __('Previous', 'my-theme'); ?>
</div>

<div class="nav-next">
    <?php previous_posts_link( __('Next', 'my-theme'); ?>
</div>

<?php wp_reset_postdata(); ?>

我有大约11个帖子,第1页显示了5个帖子,但问题是没有显示分页。

有什么想法吗? 谢谢

4 个答案:

答案 0 :(得分:3)

你可以尝试这个分页插件

http://wordpress.org/plugins/wp-pagenavi/

并在模板中添加此代码

<?php if (!function_exists('wp-pagenavi')) { wp_pagenavi(); } else { ?>
    <div class="navigation">
    <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div><?php } ?>

答案 1 :(得分:1)

我尝试了您的代码和我的分类

<?php $questions = new WP_Query(array(
    'post_type' => array('studies'),
    'post_status' => 'publish',
    'posts_per_page' => 15,    
    'paged' => get_query_var('paged'),
    'nopaging' => false,
    'tax_query' => array(
        array(
            'taxonomy' => 'studies',
            'terms' => 11
        )
    ),
    'orderby' => 'menu_order',
    'order' => 'ASC'
));


?>

<div class="grid9">
<div class="entry-content">
<?php if ($questions->have_posts()): ?>
    <ul class="list normalize">
        <?php while ($questions->have_posts()) : $questions->the_post(); ?>
            <li>
                <h3><a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></h3>                
            </li>
        <?php endwhile; ?>
    </ul>

<?php endif; ?>
<?php
wp_pagenavi( array( 'query' => $questions ) ); 
?>

这很好用,不要忘记使用wp-pagenavi插件。

答案 2 :(得分:0)

将此代码放在&#34; endif;&#34;并且在while循环结束时。

 <div class="nav-previous">
    <?php next_posts_link( __('Previous', 'my-theme'); ?>
</div>

<div class="nav-next">
    <?php previous_posts_link( __('Next', 'my-theme'); ?>
</div>

<?php wp_reset_postdata(); ?>

希望这会对你有所帮助。

答案 3 :(得分:0)

通常这对我有用: $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' => $questions->max_num_pages ) );

注意问题变量。 :-)使用完毕后,转到设置 - &gt;永久链接和保存两次。

您可以使用相同的示例找到更大的教程here

相关问题