自定义术语档案中未显示分页

时间:2014-08-20 作者:streetfire

我正在尝试对显示在术语存档中的自定义模板进行分页。

它似乎正在工作,因为当我导航到mysite/taxonomy/page/2/ 或3、4、5等。分页工作。它还显示每页正确的术语数。然而,在我的页面底部并没有显示任何实际的分页链接,听起来应该有。

我正在跟踪this tutorial 以及Codex instructions, 但我肯定错过了什么。有人能帮我弄清楚是什么吗?下面是我的简化代码。

<?php get_header(); ?>

<div class="container">
<div class="row">
  <div class="col-md-12">

<div class="row">
  <div class="col-md-9">
<h2>Index Post Type Archive </h2>
<hr/>
    <!-- Begin LG Screen View-->
    <span class="visible-lg">
          <article> 

        <div class="row">
    <?php

    if ( get_query_var( \'paged\' ) )
    $paged = get_query_var(\'paged\');
else if ( get_query_var( \'page\' ) )
    $paged = get_query_var( \'page\' );
else
    $paged = 1;

$per_page    = 12;
$number_of_terms = count( get_terms( \'100list\' ) ); // This counts the total number terms in the taxonomy with a function)
$offset      = $per_page * ( $paged - 1) ;

    $libargs=array(
      \'orderby\'           => \'name\',
         \'order\'             => \'ASC\',
        \'hide_empty\'        => 0,
        \'exclude\'           => array(16, 20, 22,25, 27, 28, 30, 4, 42, 7, 43 ), //* Enter ID\'s of parent categories to exclude from list
        \'taxonomy\'      => \'100list\',
        \'parent\'               => \'\',
        \'number\'       => $per_page,
        \'offset\'       => $offset,

        );  

        $libcats=get_categories($libargs);  

        $i = 0;

        foreach($libcats as $lc){

            if( $i % 4 == 0 ) {
             echo \'<div class="clearfix"></div>\';
            }
          $i++;
            echo \'<div class="col-lg-3">\';

            $termlink = get_term_link( $lc->slug, \'100list\' );
        ?>
       <div class="thumbnail">
            <div class="caption">
                <br/><br/>
                <h1><span class="label label-warning"><?php echo  $lc->count  ?></span></h1>
                <p> Symbols </p>
                <p> <a class="label label-default" href="<?php echo $termlink; ?>"> View Group</a> </p>
            </div>
            <!-- Get Image by Attachment ID Start-->
            <?php
                $attachment_id = get_field(\'taximage\', \'100list_\'.$lc->term_id);
                if ($attachment_id) {
                    $image = wp_get_attachment_image_src($attachment_id, \'industrygroup-img\');
                    if ($image) {
                        ?>
                        <img class="img-responsive" src="<?php echo $image[0]; ?>" />
                        <?php     } }
            else { ?>
<img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA_logo_300px_groups.jpg" alt="<?php the_title(); ?>" />

<?php } ?>
            <!-- Get Image by Attachment ID End-->
        </div>
    <small><p class="text-center"> <a href="<?php echo $termlink; ?>"> <?php echo $lc->name; ?></a> </p>  </small>

    <?php  echo \'</div>\'; } ?>

    <?php  if (function_exists("pagination")) {
$big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        \'base\'    => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\'  => \'/page/%#%\',
        \'current\' => $paged,
        \'total\'   => ceil( $number_of_terms / $per_page ) // 20 items per page

    ) );
}
    ?>

</div>
</article>
</span>
      </div> <!-- End col-md-9 -->
    <div class="col-md-3">
      <?php include("sidebar100list.php"); ?>
 </div>  <!-- End col-md-3 -->
</div><!-- End row -->
</div><!-- End Container -->
<?php get_footer(); ?>

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

我整理出的代码有一到两个问题

Big changes

  • $number_of_terms = count( get_terms( \'100list\' ) ); 替换为$number_of_terms = wp_count_terms( \'100list\' );. 原因是wp_count_terms 已存在以本机方式返回术语计数的

  • get_categories 替换为get_termsget_terms 接受offset 参数

    已删除if (function_exists("pagination")) {. 它没有任何用途,而隐藏分页链接的罪魁祸首就是它。

    已更改\'current\' => $paged,\'current\' => max( 1, get_query_var(\'paged\') ),

    清理了代码重要部分的缩进。好像要关门了</div>\'你必须把它整理一下。

    因此,下面是有效的代码。我确实在我的安装上测试了它,效果很好

    <?php get_header(); ?>
    
    <div class="container">
    <div class="row">
    <div class="col-md-12">
    
        <div class="row">
            <div class="col-md-9">
                <h2>Index Post Type Archive </h2>
                <hr/>
                <!-- Begin LG Screen View-->
                <span class="visible-lg">
                    <article> 
    
                        <div class="row">
                            <?php
    
                            if ( get_query_var( \'paged\' ) ) {
                                $paged = get_query_var(\'paged\');
                            }elseif( get_query_var( \'page\' ) ) {
                                $paged = get_query_var( \'page\' );
                            }else{
                                $paged = 1;
                            }
    
                            $per_page = 4;
                            $number_of_terms = wp_count_terms( \'100list\' ); // This counts the total number terms in the taxonomy with a function)
                            $paged_offset = ($paged - 1) * $per_page;
    
                            $libargs = array(
                                \'orderby\'           => \'name\',
                                \'order\'             => \'ASC\',
                                \'hide_empty\'        => 0,
                                \'exclude\'           => array(16, 20, 22,25, 27, 28, 30, 4, 42, 7, 43 ), //* Enter ID\'s of parent categories to exclude from list
                                \'number\'            => $per_page,
                                \'offset\'            => $paged_offset
                            );  
    
                            $libcats = get_terms( \'100list\', $libargs);  
    
                            $i = 0;
    
                            foreach($libcats as $lc){
                                if( $i % 4 == 0 ) { ?>
    
                                    <div class="clearfix"></div>
    
                                <?php }
    
                                $i++; ?>
    
                                <div class="col-lg-3">
    
                                    <?php $termlink = get_term_link( $lc, \'100list\' ); ?>
    
                                    <div class="thumbnail">
                                        <div class="caption">
                                            <br/><br/>
                                            <h1><span class="label label-warning"><?php echo  $lc->count  ?></span></h1>
                                            <p>Symbols</p>
                                            <p><a class="label label-default" href="<?php echo $termlink; ?>"> View Group</a></p>
                                        </div>
    
                                        <!-- Get Image by Attachment ID Start-->
    
                                        <?php $attachment_id = get_field(\'taximage\', \'100list_\'.$lc->term_id);
                                        if ($attachment_id) {
                                            $image = wp_get_attachment_image_src($attachment_id, \'industrygroup-img\');
                                            if ($image) { ?>
    
                                                <img class="img-responsive" src="<?php echo $image[0]; ?>" />
    
                                            <?php }   
    
                                        }else{ ?>
                                            <img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA_logo_300px_groups.jpg" alt="<?php the_title(); ?>" />
    
                                        <?php } ?>
    
                                        <!-- Get Image by Attachment ID End-->
                                    </div>      
    
                                        <small><p class="text-center"> <a href="<?php echo $termlink; ?>"> <?php echo $lc->name; ?></a></p></small>
    
                                </div>
    
                            <?php } 
    
                            $big = 999999999; // need an unlikely integer
                            echo paginate_links( 
                                array(
                                    \'base\'    => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
                                    \'format\'  => \'/page/%#%\',
                                    \'current\' => max( 1, get_query_var(\'paged\') ),
                                    \'total\'   => ceil( $number_of_terms / $per_page ) // 20 items per page
                                ) 
                            );
                            ?>
                        </div>
                    </article>
                </span>
            </div> <!-- End col-md-9 -->
    
            <div class="col-md-3">
                <?php include("sidebar100list.php"); ?>
            </div>  <!-- End col-md-3 -->
    
        </div><!-- End row -->
    </div><!-- End Container -->
    <?php get_footer(); ?>
    

SO网友:sakibmoon

问题在于function_exists("pagination"). Wordpress中没有名为pagination的函数。您正在使用该函数paginate_links(). 所以,你应该用

function_exists("paginate_links")

结束

相关推荐

Custom template pagination

我有一个自定义的帖子类型“新闻”。我创建了id为55的“News”页面和一个自定义模板文件。我想实现分页。问题是,当我访问设置了页码的新闻时,会出现错误“404未找到”。URL类似于“/新闻/页面/2/”。我使用paginate_links 函数以显示分页。如何显示正确的页面?