0

私はWordpress 3.3.1を使用しています。2012 年 4 月には 13 件の投稿があります。サイドバー (アーカイブの下) の 2012 年 4 月のリンクをクリックして、すべての記事のリストを表示しています。デフォルトでは、Wordpress は 1 ページあたり 10 個の記事を表示しており、2 ページ目にアクセスすると、ページネーションが正しく機能しています。ページごとに 5 つの投稿のみを表示できるようにしたいのですが、問題はここから始まります。私は使用しており、引数query_postsで使用されています。posts_per_page=52 つの異なるテーマでこれを試しましたが、どちらも正しく動作しません。「Twenty Ten」テーマと独自の「カスタム テーマ」を使用しています。これが私がしたことです:

21 のテーマ:

私は 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(); ?>

カスタムテーマ:

これは、ページの下部に 1、2、3、次のリンクが表示され、合計で 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">&raquo;</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(); ?>

テーマ、特にカスタム テーマのページネーションを修正するにはどうすればよいですか?

4

3 に答える 3

1

編集

使用しないでくださいquery_posts()代わりにフィルターpre_get_posts:

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()など)。

元の回答

デフォルトのargsを保持するには、カスタム引数をデフォルトの引数に連結する必要がありますが、代わりにそれらを置き換えています。query_posts()$query

これの代わりに:

//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 );
于 2012-04-18T16:05:46.250 に答える
0

while(have_posts()):カスタムテーマでは使用しません。

20 では使用しますが、20 にget_template_part( 'loop', 'archive' );
は がなくloop-archive.php、 が呼び出されますloop.php
問題にならないはずです。

カスタム テーマのループを修正してから、もう一度確認してください。

このコードはどのページにありますか? 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>
于 2012-04-18T11:21:16.830 に答える
0

あなたはおそらくすでに問題に対処していると思います。質問に対する短い答え: query_posts EVER を使用しないでください。これを使用すべきではない理由の詳細は次のとおりです

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' );
于 2012-07-09T00:53:35.130 に答える