【问题标题】:Add pagination to this Wordpress Home向这个 Wordpress 主页添加分页
【发布时间】:2015-02-13 23:41:40
【问题描述】:

伙计们!我正在为此主题主页进行分页。原代码为:

<?php
                /**
                    Blog
                **/
                $display_count = get_theme_mod('wpex_home_blog_count', '30');
                $wpex_query = new WP_Query(
                    array(
                        'post_type'         => 'post',
                        'posts_per_page'    => $display_count,
                        'no_found_rows'     => true,
                    )
                );
                if ( $wpex_query->posts && '0' != $display_count ) { ?>
                    <div id="homepage-blog" class="clr">
                        <h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
                        <?php $wpex_count=0; ?>
                        <?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
                            <?php $wpex_count++; ?>
                                <article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
                                    <?php
                                    // Display post thumbnail
                                    if ( has_post_thumbnail() ) { ?>
                                        <div class="recent-blog-entry-thumbnail">
                                            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
                                                <img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
                                            </a>
                                        </div><!-- .recent-blog-entry-thumbnail -->
                                    <?php } ?>
                                    <header>
                                        <h3 class="recent-blog-entry-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a></h3>
                                        <?php
                                        // Display post meta details
                                        wpex_post_meta() ;?>
                                    </header>
                                    <div class="recent-blog-entry-content entry clr">
                                        <?php wpex_excerpt( 18, false ); ?>
                                    </div><!-- .recent-blog-entry-content -->
                                </article><!-- .recent-blog -->
                            <?php if ( $wpex_count == '3' ) { ?>
                                <?php $wpex_count=0; ?>
                            <?php } ?>
                        <?php endforeach; ?>
                    </div><!-- #homepage-portfolio -->
                <?php } ?>

我所做的是,我把它改成了

                <div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
    <?php
    $temp = $wp_query; $wp_query= null;
    $wp_query = new WP_Query(); $wp_query->query('showposts=4' . '&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <?php $wpex_count++; ?>
                                <article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
                                    <?php
                                    // Display post thumbnail
                                    if ( has_post_thumbnail() ) { ?>
                                        <div class="recent-blog-entry-thumbnail">
                                            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
                                                <img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
                                            </a>
                                        </div><!-- .recent-blog-entry-thumbnail -->
                                    <?php } ?>
                                    <header>
                                        <h3 class="recent-blog-entry-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a></h3>
                                    <ul class="post-meta clr">
                                    <li class="meta-date">
            <?php _e('Posted on','wpex'); ?>
            <span class="meta-date-text"><?php echo get_the_date(); ?></span>
        </li>
        </ul>
                                    </header>
                                    <div class="recent-blog-entry-content entry clr">
                                        <?php wpex_excerpt( 18, false ); ?>
                                    </div><!-- .recent-blog-entry-content -->
                                </article><!-- .recent-blog -->
                            <?php if ( $wpex_count == '3' ) { ?>
                                <?php $wpex_count=0; ?>
                            <?php } ?>

    <?php endwhile; ?>
</div>

 <?php wp_pagenavi();?>
<?php wp_reset_postdata(); ?>

</article>

这似乎在第一页上有效,但是当我转到第 2 页时,它会在整个页面的副本之后的副本中给出一个副本,就像一个无限的页面一样。这不是它应该如何工作的。

你知道这里出了什么问题吗?这对你有意义吗?

编辑:

我尝试了以下方法。现在第一页看起来不错,但是当我在分页上按第二页时,它的输出与第一页相同。

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

                    <div id="homepage-blog" class="clr">
                        <h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
                        <?php $wpex_count=0; ?>

                        <?php if (have_posts()) : ?>

                        <?php while (have_posts()) : the_post(); ?>


                            <?php $wpex_count++; ?>
                                <article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
                                    <?php
                                    // Display post thumbnail
                                    if ( has_post_thumbnail() ) { ?>
                                        <div class="recent-blog-entry-thumbnail">
                                            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
                                                <img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
                                            </a>
                                        </div><!-- .recent-blog-entry-thumbnail -->
                                    <?php } ?>
                                    <header>
                                        <h3 class="recent-blog-entry-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a></h3>
                                        <ul class="post-meta clr">
                                            <li class="meta-date"><?php _e('Posted on','wpex'); ?> <span class="meta-date-text"><?php echo get_the_date(); ?></span>                                                </li>
                                        </ul>

                                    </header>
                                    <div class="recent-blog-entry-content entry clr">
                                        <?php wpex_excerpt( 18, false ); ?>
                                    </div><!-- .recent-blog-entry-content -->
                                </article><!-- .recent-blog -->
                            <?php if ( $wpex_count == '3' ) { ?>
                                <?php $wpex_count=0; ?>
                            <?php } ?>
                        <?php endwhile; ?>
                        <?php wp_pagenavi();?>
                    </div><!-- #homepage-portfolio -->

                <?php endif; ?>
                <?php wp_reset_query(); ?>
            </article><!-- #post -->
        <?php endwhile; ?>

完整的原始代码:

<div id="primary" class="content-area clr">
    <div id="content" class="site-content" role="main">
        <?php while ( have_posts() ) : the_post(); ?>
            <article class="homepage-wrap clr">
                <?php
                /**
                    Post Content
                **/ ?>
                <?php if ( get_the_content() !== '' ) { ?>
                    <div id="homepage-content" class="entry clr">
                        <?php the_content(); ?>
                    </div><!-- .entry-content -->
                <?php } ?>
                <?php
                /**
                    Features
                **/
                $wpex_query = new WP_Query(
                    array(
                        'order'             => 'ASC',
                        'orderby'           => 'menu_order',
                        'post_type'         => 'features',
                        'posts_per_page'    => '-1',
                        'no_found_rows'     => true,
                    )
                );
                if ( $wpex_query->posts ) { ?>
                    <div id="homepage-features" class="clr">
                        <?php $wpex_count=0; ?>
                        <?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
                            <?php $wpex_count++; ?>
                                <?php get_template_part( 'content-features', get_post_format() ); ?>
                            <?php if ( $wpex_count == '4' ) { ?>
                                <?php $wpex_count=0; ?>
                            <?php } ?>
                        <?php endforeach; ?>
                    </div><!-- #homepage-features -->
                <?php } ?>
                <?php wp_reset_postdata(); ?>
                <?php
                /**
                    Portfolio
                **/
                $display_count = get_theme_mod('wpex_home_portfolio_count', '8');
                $wpex_query = new WP_Query(
                    array(
                        'post_type'         => 'portfolio',
                        'posts_per_page'    => $display_count,
                        'no_found_rows'     => true,
                        'tax_query'         => wpex_home_portfolio_taxonomy(),
                    )
                );
                if ( $wpex_query->posts && '0' != $display_count ) { ?>
                    <div id="homepage-portfolio" class="clr">
                        <h2 class="heading"><span><?php _e( 'Recent Work', 'wpex' ); ?></span></h2>
                        <?php $wpex_count=0; ?>
                        <?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
                            <?php $wpex_count++; ?>
                                <?php get_template_part( 'content-portfolio', get_post_format() ); ?>
                            <?php if ( $wpex_count == '4' ) { ?>
                                <?php $wpex_count=0; ?>
                            <?php } ?>
                        <?php endforeach; ?>
                    </div><!-- #homepage-portfolio -->
                <?php } ?>
                <?php wp_reset_postdata(); ?>
                <?php
                /**
                    Blog
                **/
                $display_count = get_theme_mod('wpex_home_blog_count', '30');
                $wpex_query = new WP_Query(
                    array(
                        'post_type'         => 'post',
                        'posts_per_page'    => $display_count,
                        'no_found_rows'     => true,
                    )
                );
                if ( $wpex_query->posts && '0' != $display_count ) { ?>
                    <div id="homepage-blog" class="clr">
                        <h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
                        <?php $wpex_count=0; ?>
                        <?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
                            <?php $wpex_count++; ?>
                                <article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
                                    <?php
                                    // Display post thumbnail
                                    if ( has_post_thumbnail() ) { ?>
                                        <div class="recent-blog-entry-thumbnail">
                                            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
                                                <img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
                                            </a>
                                        </div><!-- .recent-blog-entry-thumbnail -->
                                    <?php } ?>
                                    <header>
                                        <h3 class="recent-blog-entry-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a></h3>
                                        <?php
                                        // Display post meta details
                                        wpex_post_meta() ;?>
                                    </header>
                                    <div class="recent-blog-entry-content entry clr">
                                        <?php wpex_excerpt( 18, false ); ?>
                                    </div><!-- .recent-blog-entry-content -->
                                </article><!-- .recent-blog -->
                            <?php if ( $wpex_count == '3' ) { ?>
                                <?php $wpex_count=0; ?>
                            <?php } ?>
                        <?php endforeach; ?>
                    </div><!-- #homepage-portfolio -->
                <?php } ?>
                <?php wp_reset_postdata(); ?>
            </article><!-- #post -->
            <?php comments_template(); ?>
        <?php endwhile; ?>
    </div><!-- #content -->
</div><!-- #primary -->

【问题讨论】:

  • 也尝试使用默认的分页代码,而不是使用那个插件&lt;?php previous_posts_link(); ?&gt; 和&lt;?php next_posts_link(); ?&gt; 看看它是怎么回事。
  • 这并没有做出改变,它不是导致问题的插件,效果很好。默认下一个/上一个链接的行为相同。 @sdcr
  • 对不起@sdcr,超级烦人,但没有。它没有改变。
  • @sdcr 我刚刚添加了该页面的整个(原始)代码 :) 谢谢。
  • 您有网站的实时网址吗?在看到整个代码之后,我认为你不应该在那里放置分页,你可以使用 CSS。例如你可以设置它只显示16个项目,你设置4个为一组,每次只显示4个,其余的隐藏在CSS中。而且我认为在这种情况下分页不会起作用。你想聊天吗?

标签: php wordpress loops pagination wordpress-theming


【解决方案1】:

尝试在外部声明$paged,并在query_posts 中调用它

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $args = array(
        'posts_per_page' => 4
            );
         query_posts($args . '&paged=' . $paged); 

【讨论】:

  • 感谢您的回复。我试了一下,但没有结果。问题保持不变。
【解决方案2】:

我自己可能已经找到了答案,显然在首页上使用分页时会有所不同。我用过这个,它似乎工作了!:

if( is_front_page() ){
    $paged = (get_query_var('page')) ? get_query_var('page') : 1; 
} else {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    相关资源
    最近更新 更多