【问题标题】:Pagination WordPress on the home page主页上的分页 WordPress
【发布时间】:2019-06-29 03:30:26
【问题描述】:

我在尝试让分页在我正在处理的网站的主页上工作时遇到问题。

这是我正在使用的代码,当然是简化的:

这个怎么分页?

<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
    <?php query_posts($args); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();    ?>

            <article class="col-md-12">
                <a href="<?php the_permalink(); ?>" >
                    <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                    <div class="entry entry-table">
                        <div class="title">
                          <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                          <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                       <p><?php echo get_excerpt(228, 'content'); ?>
                        <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                       </p>
                        </div>
                    </div>

            </article>

        <?php endwhile; endif; ?>

【问题讨论】:

    标签: php wordpress pagination wordpress-theming


    【解决方案1】:
    <?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
        <?php query_posts($args); ?>
        <?php if (have_posts()) : while (have_posts()) : the_post();    ?>
    
                <article class="col-md-12">
                    <a href="<?php the_permalink(); ?>" >
                        <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                        <div class="entry entry-table">
                            <div class="title">
                              <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                              <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                           <p><?php echo get_excerpt(228, 'content'); ?>
                            <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                           </p>
                            </div>
                        </div>
    
                </article>
    
            <?php endwhile; ?> 
    <div class="navigation">
      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
    </div>
    <?php endif; ?>
    

    【讨论】:

    • 仅显示最后 3 个帖子,即使在第 2 页上也是如此
    • @TomaszB。尝试在代码末尾添加&lt;?php wp_reset_query(); ?&gt;。也许是查询缓存
    【解决方案2】:

    首先,您必须正确设置查询:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
    

    其次,您需要在循环之外添加分页链接

    <div class="navigation">
      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
    </div>
    

    整个代码可以是这样的:

    <?php
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
    ?>
        <?php query_posts($args); ?>
        <?php if (have_posts()) : while (have_posts()) : the_post();    ?>
    
                <article class="col-md-12">
                    <a href="<?php the_permalink(); ?>" >
                        <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                        <div class="entry entry-table">
                            <div class="title">
                              <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                              <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                           <p><?php echo get_excerpt(228, 'content'); ?>
                            <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                           </p>
                            </div>
                        </div>
    
                </article>
    
            <?php endwhile; ?> 
    <div class="navigation">
      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
    </div>
    <?php endif; ?>
    

    【讨论】:

    • 仅显示最后 3 个帖子,即使在第 2 页上也是如此
    【解决方案3】:

    问题已解决:

    <?php
    global $paged, $wp_query, $wp;
    $args1 = array('cat' => '3, 7, 10, 12');
    
    $args = wp_parse_args($wp->matched_query);
    if ( !empty ( $args['paged'] ) && 0 == $paged ) {
    $wp_query->set('paged', $args['paged']);
    $paged = $args['paged'];
    }
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('paged='.$paged.'&showposts=3&cat=3,7,10,12');
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <article class="col-md-12">
                    <a href="<?php the_permalink(); ?>" >
                        <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                        <div class="entry entry-table">
                            <div class="title">
                              <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                              <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                           <p><?php echo get_excerpt(228, 'content'); ?>
                            <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                           </p>
                            </div>
                        </div>
    
                </article>
     <?php endwhile; ?>
     <?php wp_pagenavi(); ?>
     <?php $wp_query = null; $wp_query = $temp;?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      相关资源
      最近更新 更多