自定义帖子wordpress上的分页

时间:2013-01-29 08:55:02

标签: wordpress pagination wordpress-theming custom-post-type

<?php query_posts('showposts=5&post_type=html5-blank'); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" class="clearfix" <?php post_class(); ?>>
           //Loop Here
    <!-- /Article -->

<?php endwhile; ?>
<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

我的分页链接出现Page doesn't exist错误。 结果链接为:www.mywebsite.com/blog/page/2/ 这是一个博客页面。我编辑了循环代码。

HELP .........

4 个答案:

答案 0 :(得分:2)

我的主页(index.php)遇到类似的问题,列出了帖子。我一直找不到页面。 https://codex.wordpress.org/Pagination中的说明为我工作了www.domain.com/page/2/。

首先从模板文件中删除query_posts部分(index.php,category.php)

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
query_posts($args);

然后在functions.php中添加以下内容

function my_post_queries( $query ) {
    // do not alter the query on wp-admin pages and only alter it if it's the main query
    if (!is_admin() && $query->is_main_query()){
        // alter the query for the home and category pages
        if(is_home()){
            $query->set('posts_per_page', 3);
        }
        if(is_category()){
            $query->set('posts_per_page', 3);
        }
    }
]
add_action( 'pre_get_posts', 'my_post_queries' );

注意:HTML5空白主题和下划线主题都给了我404分页错误。上述解决方案为两个主题都有分页效果。

答案 1 :(得分:1)

我宁愿使用WP_Query并使用paged分页参数。在此处详细了解:WP_Query#Pagination_Parameters

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
    array(
        'post_type' => 'html5-blank',
        'posts_per_page' => 5,
        'paged'=>$paged
    )
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
    <article id="post-<?php the_ID(); ?>" class="clearfix" <?php post_class(); ?>>
           //Loop Here
    <!-- /Article -->

<?php endwhile; endif; ?>
<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

请让我知道:)。

第二个例子:

global $post;
global $paged, $wp_query;
$args = array( 'posts_per_page' => 5, 'post_type' => 'html5-blank', 'paged' => $paged );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
    setup_postdata($post);
    // loop
    the_title(); // or what it is needed inside the loop
endforeach;
if (  $wp_query->max_num_pages > 1 ) :
    previous_posts_link('&laquo; Newer');
    next_posts_link('Older &raquo;');
endif;

答案 2 :(得分:0)

<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(  
            'post_type' => 'html5-blank', //Post type
            'posts_per_page' => 5, //How many post u want to display per page
            'paged' => $paged                      
            );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
    $the_query->the_post();

    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
    <img src="<?=$url?>" width="350" height="350" class="thumbnail imageRight"/>
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <p><?php the_excerpt(); ?></p>

<?php } } ?>
<div class="pagination">
<?php             
    global $wp_query;

    $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' => $wp_query->max_num_pages
    ) );
?>
</div>

答案 3 :(得分:0)

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination

$the_query = new WP_Query( array( 
    'post_type' => 'YOUR_POST_TYPE_NAME',
    'paged' => $paged,
    'posts_per_page' => 5) 
);

while ( $the_query->have_posts() ) : $the_query->the_post();
    // YOUR CODE
endwhile;


echo '<nav>';
echo  '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo  '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";


wp_reset_postdata(); // Rest Data

请您尝试上面的代码吗?