Wordpress Pagination Problem

时间:2012-11-14 作者:Erik Dewangga

我想在wordpress索引中设置分页。php,但它不工作。在我的索引中。php有3个显示自定义帖子的循环部分,我想在索引中的Blog/recent post部分循环中设置分页。php。这是索引中的代码。php

<?php
/**
* @package WordPress
* @subpackage Adapt Theme
*/
$options = get_option( \'adapt_theme_settings\' );
?>
<?php get_header(); ?>

<div class="home-wrap clearfix">

<!-- Homepage tagline -->
<?php if(get_bloginfo(\'description\')) { ?>
<aside id="home-tagline">
    <?php echo bloginfo(\'description\'); ?>
</aside>
<!-- /home-tagline -->
<?php } ?>

<!-- /Homepage Slider -->
<?php get_template_part( \'includes/slides\' ); ?> 

<!-- Homepage Highlights -->
<?php
//get post type ==> hp highlights
    global $post;
    $args = array(
        \'post_type\' =>\'hp_highlights\',
        \'numberposts\' => \'-1\'
    );
    $highlight_posts = get_posts($args);
?>
<?php if($highlight_posts) { ?>        


<section id="home-highlights" class="clearfix">
    <?php
    $count=0;
    foreach($highlight_posts as $post) : setup_postdata($post);
    $count++;
    //get img
    $feat_img = wp_get_attachment_image_src( get_post_thumbnail_id(), \'full-size\');
    //meta
    $highlights_url = get_post_meta($post->ID, \'adapt_highlights_url\', TRUE);
    ?>

    <article class="hp-highlight <?php if($count == \'4\') { echo \'remove-margin\'; }   if($count == \'3\') { echo \' responsive-clear\'; } ?>">
        <h2>
        <?php if($feat_img) { ?><span><img src="<?php echo $feat_img[0]; ?>" alt="<?php the_title(); ?>" /></span><?php } ?>
        <?php if($highlights_url) { ?>
            <a href="<?php echo $highlights_url; ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php } else { the_title(); } ?>
        </h2>

        <?php the_excerpt(); ?>
    </article>
    <!-- /hp-highlight -->

    <?php
    if($count == \'4\') { echo \'<div class="clear"></div>\'; $count=0; }
    endforeach; ?>
</section>
<!-- /home-projects -->         
<?php } ?>


<!-- Recent Portfolio Items -->
<?php
//get post type ==> portfolio
    global $post;
    $args = array(
        \'post_type\' =>\'portfolio\',
        \'numberposts\' => \'4\'
    );
    $portfolio_posts = get_posts($args);
?>
<?php if($portfolio_posts) { ?>        
    <section id="home-projects" class="clearfix">
        <h2 class="heading"><span><?php if(!empty($options[\'recent_work_text\'])) { echo $options[\'recent_work_text\']; } else { _e(\'Recent Work\',\'adapt\'); }?></span></h2>

        <?php
        $count=0;
        foreach($portfolio_posts as $post) : setup_postdata($post);
        $count++;
        //get portfolio thumbnail
        $feat_img = wp_get_attachment_image_src(get_post_thumbnail_id(), \'grid-thumb\');
        ?>

        <?php if ($feat_img) {  ?>
        <div class="portfolio-item <?php if($count == \'4\') { echo \'remove-margin\'; } if($count == \'3\') { echo \' responsive-clear\'; } ?>">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $feat_img[0]; ?>" height="<?php echo $feat_img[2]; ?>" width="<?php echo $feat_img[1]; ?>" alt="<?php echo the_title(); ?>" />
            <div class="portfolio-overlay"><h3><?php echo the_title(); ?></h3></div><!-- portfolio-overlay -->
            </a>
        </div>
        <!-- /portfolio-item -->
        <?php } ?>

        <?php
        if($count == \'4\') { echo \'<div class="clear"></div>\'; $count=0; }
        endforeach; ?>
    </section>
    <!-- /home-projects -->         
<?php } ?>


<!-- Recent Blog Posts -->
<?php
//get post type ==> regular posts
    global $post;
    $args = array(
        \'post_type\' =>\'post\',
        \'numberposts\' => \'12\'
    );
    $blog_posts = get_posts($args);
?>
<?php if($blog_posts) { ?>        
    <section id="home-posts" class="clearfix">
        <h2 class="heading"><span><?php if(!empty($options[\'recent_work_text\'])) { echo $options[\'recent_news_text\']; } else { _e(\'Recent News\',\'adapt\'); }?></span></h2>
        <?php
        $count=0;
        foreach($blog_posts as $post) : setup_postdata($post);
        $count++;
        //get portfolio thumbnail
        $feat_img = wp_get_attachment_image_src(get_post_thumbnail_id(), \'grid-thumb\');
        ?>


        <article class="home-entry <?php if($count == \'4\') { echo \'remove-margin\'; } if($count == \'3\') { echo \' responsive-clear\'; } ?>">
            <div class="home-entry-image">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo catch_that_image() ?>"></a>
    </div>
            <div class="home-entry-description">
                <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo the_title(); ?></a></h3>
                <?php echo excerpt(\'15\'); ?>
            </div> 
            <!-- /home-entry-description -->
        </article>
        <!-- /home-entry-->
        <?php
        if($count == \'4\') { echo \'<div class="clear"></div>\'; $count=0; }
        endforeach; ?>
    </section>
    <!-- /home-posts -->
<!-- AREA WHERE I WANT PUT PAGINATE -->
<?php } ?>

</div>
<!-- END home-wrap -->   
<?php get_footer(); ?>
我想要分页的区域位于“我想要分页的区域”(你可以看到上面的注释标记),但它不起作用,如果我单击第2页,它总是显示第一页的帖子(没有更改)。

下面是文件分页的代码。包含在函数文件中的php

<?php
/**
 * @package WordPress
* @subpackage Adapt Theme
*/

//pagination function
function pagination($pages = \'\', $range = 4)
{
 $showitems = ($range * 2)+1; 

 global $paged;
 if(empty($paged)) $paged = 1;

 if($pages == \'\')
 {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if(!$pages)
     {
         $pages = 1;
     }
 }  

 if(1 != $pages)
 {
     echo "<div class=\\"pagination clearfix\\">";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>&laquo;</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo;</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class=\\"current\\">".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a>";
         }
     }

     if ($paged < $pages && $showitems < $pages) echo "<a href=\\"".get_pagenum_link($paged + 1)."\\">&rsaquo;</a>";
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>&raquo;</a>";
     echo "</div>\\n";
 }
}
?>
注意:我使用wpexplorer中的模板“Adapt”wordpress主题。我

1 个回复
SO网友:totels

创建自定义帖子列表时,分页不会自动进行,您需要将分页参数传递到get_posts 呼叫有关详细信息,请访问WP_Query codex page.

对于要分页的查询,您需要添加如下内容\'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; 到您的$args. 如果我的阅读正确,我看不到您实际上在模板中调用分页显示函数,您应该能够添加<?php pagination(); ?> 你在哪里<!-- AREA WHERE I WANT PUT PAGINATE --> [原文如此]。

可能有一些关于在theme developers website, 我建议你在那里多读一些,甚至有一个你可以复制的例子。

结束

相关推荐