0
<?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/ 这是一个博客页面。我已经编辑了循环代码。

帮助.........

4

4 回答 4

2

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

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

$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 分页错误。上述解决方案使分页适用于两个主题。

于 2013-03-09T05:23:00.337 回答
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;
于 2013-01-29T09:28:31.127 回答
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>
于 2014-12-25T17:07:38.687 回答
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

你能试试上面的代码吗?

于 2016-09-11T16:47:33.350 回答