自定义类别模板分页问题

时间:2016-08-24 作者:jrcollins

我自定义了主题的类别模板,以显示当前类别的子类别,而不是帖子。除以下错误外,一切正常:Fatal error: Call to undefined function codilight_lite_custom_paginate() in...

我可以从模板中删除该函数,但如果可能的话,我想保留它,这样我就可以选择为具有大量子类别的类别的结果分页。

是否有任何方法可以修改该函数,使其与自定义模板一起工作?

功能如下:

if ( ! function_exists( \'codilight_lite_custom_paginate\' ) ) :
/**
 * Retrieve paginated link for archive post pages.
 */
function codilight_lite_custom_paginate() {
    global $wp_query;
    $total_pages = $wp_query->max_num_pages;
    $big         = 999999999;
    $translated  = __( \'Page\', \'codilight-lite\' );

    if ($total_pages > 1){
        $current_page = max(1, get_query_var(\'paged\'));
        echo \'<div class="ft-paginate">\';
        echo paginate_links(array(
            \'base\'               => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\'             => \'?paged=%#%\',
            \'prev_next\'          => True,
            \'prev_text\'          => \'<i class="fa fa-angle-left"></i>\',
            \'next_text\'          => \'<i class="fa fa-angle-right"></i>\',
            \'current\'            => $current_page,
            \'total\'              => $total_pages,
            \'before_page_number\' => \'<span class="screen-reader-text">\'.$translated.\' </span>\'
        ));
        //echo \'<span class="total-pages">Page \'. $current_page .\' of \'. $total_pages .\'</span>\';
        printf( \'<span class="total-pages">\'. esc_html__( \'Page %1$s of %2$s\', \'codilight-lite\' ) .\'</span>\', $current_page, $total_pages );
        echo \'</div>\';
    }
}
endif;
。。。这是我的自定义类别模板:

<?php
/**
 * Category Template: Custom
 */

get_header(); ?>
    <div id="content" class="site-content container <?php echo codilight_lite_sidebar_position(); ?>">
        <div class="content-inside">
            <div id="primary" class="content-area">
                <main id="main" class="site-main" role="main">
<?php 
        $cat = get_category( get_query_var( \'cat\' ) );
        $cat_id = $cat->cat_ID;
        $child_categories=get_categories(
        array( 
        \'parent\' => $cat_id,
        // Uncomment the below line if you want empty category to appear on the list. 
        // \'hide_empty\'   => 0
    )
); 
        if (!empty($child_categories)) : $count = 0; ?>

                    <header class="page-header">
                        <?php
                            the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
                            the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' );
                        ?>
                    </header><!-- .page-header -->

                    <?php
                        echo \'<div class="block1 block1_grid">\';
                        echo \'<div class="row">\';

                    foreach ( $child_categories as $child ){ $count++;
                    include( locate_template( \'template-parts/content-grid.php\' ) );

                    if ( $count % 2 == 0 ) {
                                echo \'</div>\';
                                echo \'<div class="row">\';
                    }
                    }
                        echo \'</div>\';
                        echo \'</div>\';
                    ?>

                <?php else : ?>

                    <?php get_template_part( \'template-parts/content\', \'none\' ); ?>

                <?php endif; ?>

                </main><!-- #main -->
            </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:CodeMascot 整理而成

嗯,修改这个codilight_lite_custom_paginate() 功能不是一个好的选择。原因如果它被修改,那么它可能会导致其他页面出现问题。所以如果你想分页archive.php 子类别foreach 循环,然后移除codilight_lite_custom_paginate() 功能和自定义archive.php 页面如下-

<?php
/**
 * Category Template: Custom
 */

get_header(); ?>
<div id="content" class="site-content container <?php echo codilight_lite_sidebar_position(); ?>">
    <div class="content-inside">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <?php
            $cat = get_category( get_query_var( \'cat\' ) );
            $cat_id = $cat->cat_ID;
            $child_categories=get_categories(
                array(
                    \'parent\' => $cat_id,
                    // Uncomment the below line if you want empty category to appear on the list.
                    \'hide_empty\'   => 0
                )
            );

            // Variables for pagination
            $page = isset($_GET[\'page\'])?intval($_GET[\'page\']-1):0;
            $per_page_cat_num = 10;
            $number_of_pages = intval(count($child_categories)/$per_page_cat_num)+1;

            if (!empty($child_categories)) : $count = 0; ?>

                <header class="page-header">
                    <?php
                    the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
                    the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' );
                    ?>
                </header><!-- .page-header -->
                <?php
                echo \'<div class="block1 block1_grid">\';
                echo \'<div class="row">\';

                foreach ( array_slice($child_categories, $page*$per_page_cat_num, $per_page_cat_num) as $child ){
                    $count++;
                    include( locate_template( \'loop-templates/content-grid.php\' ) );

                    if ( $count % 2 == 0 ) {
                        echo \'</div>\';
                        echo \'<div class="row">\';
                    }
                }
                echo \'</div>\';
                echo \'</div>\';
                ?>

            <?php else : ?>

                <?php get_template_part( \'template-parts/content\', \'none\' ); ?>

            <?php endif; ?>

            <!-- Pagination HTML starts here. Style it how ever you want -->
            <ul id=\'paginator\'>
                <?php
                for( $i=1; $i<$number_of_pages; $i++){?>
                    <li><a href=\'./?page=<?php echo $i ?>\'><?php echo $i ?></a></li>
                <?php } ?>
            </ul>
            <!-- Pagination HTML ends here -->

        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
在这里$per_page_cat_num 保存每页要显示的子类别数。我已将其默认值指定为10。将其更改为您的上下文。