我正在使用Wordpress 3.3.1。我在2012年4月份有13个帖子。我点击侧栏中的2012年4月链接(在Archives下)以显示所有文章的列表。默认情况下,Wordpress每页显示10篇文章,当我访问第2页时,分页工作正常。我希望每页只能显示5个帖子,问题从这里开始。我正在使用query_posts
并在参数中使用posts_per_page=5
。我在2个不同的主题上尝试了这个,但它们都无法正常工作。我正在使用“二十”主题和我自己的“自定义主题”。这是我做的:
二十几岁:
我尝试了两组代码,在使用一组代码时禁用另一组代码。我在if ( have_posts() ) the_post();
之前添加了它们。因为有13篇文章,2页导航效果很好。但是当我尝试访问最后一页时,它给出了404错误。以下是此主题的完整代码:
<?php
/**
* The template for displaying Archive pages.
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php
//1st experiment
//query_posts( array( 'posts_per_page' => 5, 'paged' => get_query_var('paged') ) );
//2nd experiment
$args = array(
'posts_per_page' => 5,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args); //Shows a 404 page when visiting last page
/* Queue the first post, that way we know
* what date we're dealing with (if that is the case).
*
* We reset this later so we can run the loop
* properly with a call to rewind_posts().
*/
if ( have_posts() )
the_post();
?>
<h1 class="page-title">
<?php if ( is_day() ) : ?>
<?php printf( __( 'Daily Archives: <span>%s</span>', 'twentyten' ), get_the_date() ); ?>
<?php elseif ( is_month() ) : ?>
<?php printf( __( 'Monthly Archives: <span>%s</span>', 'twentyten' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'twentyten' ) ) ); ?>
<?php elseif ( is_year() ) : ?>
<?php printf( __( 'Yearly Archives: <span>%s</span>', 'twentyten' ), get_the_date( _x( 'Y', 'yearly archives date format', 'twentyten' ) ) ); ?>
<?php else : ?>
<?php _e( 'Blog Archives', 'twentyten' ); ?>
<?php endif; ?>
</h1>
<?php
/* Since we called the_post() above, we need to
* rewind the loop back to the beginning that way
* we can run the loop properly, in full.
*/
rewind_posts();
/* Run the loop for the archives page to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-archive.php and that will be used instead.
*/
get_template_part( 'loop', 'archive' );
?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
CUSTOM THEME:
更糟糕的是,页面确实在页面底部显示了1,2,3,Next链接,并且总共只显示了5个帖子,但是当我点击2时,分页显示了相同的帖子作为第一页中的那些。当我点击“3”链接或“下一步”链接时,我看到404错误。请注意,使用的分页技术在此主题中有所不同。我已经尝试了很多组合,但没有一个有用。我将这些实验留在评论意见中。
<?php get_header(); ?>
<div id="container">
<div id="content">
<?php the_post(); ?>
<?php if ( is_day() ) : ?>
<h1 class="page-title"><?php printf( __( 'Daily Archives: <span>%s</span>', 'your-theme' ), get_the_time(get_option('date_format')) ) ?></h1>
<?php elseif ( is_month() ) : ?>
<h1 class="page-title"><?php printf( __( 'Monthly Archives: <span>%s</span>', 'your-theme' ), get_the_time('F Y') ) ?></h1>
<?php elseif ( is_year() ) : ?>
<h1 class="page-title"><?php printf( __( 'Yearly Archives: <span>%s</span>', 'your-theme' ), get_the_time('Y') ) ?></h1>
<?php elseif ( isset($_GET['paged']) && !empty($_GET['paged']) ) : ?>
<h1 class="page-title"><?php _e( 'Blog Archives', 'your-theme' ) ?></h1>
<?php endif; ?>
<?php rewind_posts(); ?>
<?php //$posts=query_posts($query_string . '&showposts=' . get_option('posts_per_page')); ?>
<?php //$posts=query_posts($query_string . '&showposts=5'); ?>
<?php
/*global $query_string;*/
//query_posts($query_string . "&posts_per_page=5");
//query_posts($query_string.'&showposts=5');
/*$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("paged=$paged");
*/
/*global $query_string;
parse_str( $query_string, $my_query_array );
$paged = ( isset( $my_query_array['paged'] ) && !empty( $my_query_array['paged'] ) ) ? $my_query_array['paged'] : 1;
query_posts('post_type=post&posts_per_page=5&paged='.$paged);*/
/*$args = array(
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => 5
);
//$query = new WP_Query( $args );
$query = new WP_Query( 'posts_per_page=3' );*/
//query_posts( array( 'posts_per_page' => 10, 'paged' => get_query_var('paged') ) );
/*if(!is_home() ){
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 5;
query_posts($args);
}*/
// The Query
/*global $wp_query;
query_posts(
array_merge(
array(
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5
),
$wp_query->query
)
);
*/
//remove_page_from_query_string();
//custom_query_posts();
?>
<?php
//The Query
/*$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'paged'=>$paged, //Pulls the paged function into the query
'posts_per_page'=>5, //Limits the amount of posts on each page
);
query_posts($args);*/
// Reset Query
//wp_reset_query();
//query_posts('posts_per_page=5');
/*global $query_string;
query_posts( $query_string . '&posts_per_page=5' );
*/
/* $args = array(
'posts_per_page' => 5,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
*/
//query_posts(array('posts_per_page' => 5, 'paged' => get_query_var('page')));
query_posts("posts_per_page=5");
?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="entry-meta">
<span class="meta-prep meta-prep-author"><?php _e('By ', 'your-theme'); ?></span>
<span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>
<span class="meta-sep"> | </span>
<span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'your-theme'); ?></span>
<span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>
<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?>
</div><!-- .entry-meta -->
<div class="entry-summary">
<?php the_excerpt( __( 'Continue reading <span class="meta-nav">»</span>', 'your-theme' ) ); ?>
</div><!-- .entry-summary -->
<div class="entry-utility">
<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'your-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>
<span class="meta-sep"> | </span>
<?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'your-theme' ) . '</span>', ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>
<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'your-theme' ), __( '1 Comment', 'your-theme' ), __( '% Comments', 'your-theme' ) ) ?></span>
<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t\n" ) ?>
</div><!-- #entry-utility -->
</div><!-- #post-<?php the_ID(); ?> -->
<?php endwhile; ?>
<div class="pagination">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</div><!--END .pagination-->
<?php //Tried enabling/disabling this, but does not help
//wp_reset_query(); // Restore global post data
?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
如何修复主题中的分页,最重要的是在自定义主题中?
答案 0 :(得分:1)
请不要使用query_posts()
。 Filter pre_get_posts
instead:
function so10213400_filter_pre_get_posts( $query ) {
if ( is_main_query() && is_month() ) {
$query->query_vars['posts_per_page'] = 5;
}
return;
}
add_filter( 'pre_get_posts', 'so10213400_filter_pre_get_posts' );
请注意,回调可确保查询是给定页面的主查询,并且当前上下文是月份归档。如果您希望针对不同的上下文采用相同的行为,请将is_month()
更改为适当的内容(例如is_date()
或is_archive()
,或者! is_home()
等。)
您需要将自定义query_posts()
args连接到默认$query
args in order to preserve the default args ,而是替换它们
而不是:
//2nd experiment
$args = array(
'posts_per_page' => 5,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args); //Shows a 404 page when visiting last page
...试试这个:
$args = array(
'posts_per_page' => 5,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
global $wp_query;
$merged_args = array_merge( $wp_query->query, $args );
query_posts( $merged_args );
答案 1 :(得分:0)
您不在自定义主题中使用while(have_posts()):
。
在二十九岁时,你使用get_template_part( 'loop', 'archive' );
但是,Tweten没有loop-archive.php
,会调用loop.php
应该不是问题。
在自定义主题中修复the loop,然后重新检查。
您在哪个页面上有此代码? archive.php?
的修改
<div id="pagination">
<div id="pagination-previous" class="button"><?php previous_posts_link('previous'); ?></div>
<div id="pagination-next" class="button"><?php next_posts_link('next'); ?></div>
</div>
答案 2 :(得分:0)
我意识到你可能已经处理过这个问题了。问题的简短回答:不要使用query_posts EVER 。以下是您不应该使用它的详细信息: http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
pre_get_posts 挂钩似乎适合此
function my_pre_posts( $query = false ) {
// Bail if not home, not a query, not main query
if ( ! is_home() || ! is_a( $query, 'WP_Query' ) || ! $query->is_main_query() )
return;
// Exclude featured posts from the main query
$query->set( 'posts_per_page', 5 );
// Note the we aren't returning anything.
// 'pre_get_posts' is a byref action; we're modifying the query directly.
}
add_action( 'pre_get_posts', 'my_pre_posts' );