有人能告诉我如何用下面的代码创建函数吗?它用于在自定义类别模板中显示页面导航链接。正如您所看到的,这是一个相当大的代码块,我想将其包装在一个函数中,这样我就可以在模板中仅使用一行代码生成链接。
这是:
<?php
// Start of Pagination
$total_childs_query = get_categories( array( \'parent\' => $cat_id, \'hide_empty\' => \'0\' ));
$total_terms = count( $total_childs_query );
$pages = ceil($total_terms/$catnum);
$base_url = get_term_link( $cat_id, get_query_var( \'taxonomy\' ) );
// if there\'s more than one page
if( $pages > 1 ):
echo \'<div class="ft-paginate">\';
echo \'<nav class="navigation pagination" role="navigation">\';
echo \'<h2 class="screen-reader-text">Posts navigation</h2>\';
echo \'<div class="nav-links">\';
// if we\'re not on the first page, print the previous-link
if ( $catpage > 1 ) {
$prevpage = $catpage - 1;
if ( $prevpage > 1 ) {
echo \'<a class="prev page-numbers" href="\' . $base_url . \'/page/\' . $prevpage . \'"><i class="fa fa-angle-left"></i></a>\';
}
else {
echo \'<a class="prev page-numbers" href="\' . $base_url . \'"><i class="fa fa-angle-left"></i></a>\';
}
}
for ($pagecount=1; $pagecount <= $pages; $pagecount++):
//set class
$class = "page-numbers";
if ( $pagecount == $catpage ) {
$class .= " current";
}
if ( $pagecount == $catpage ) {
echo \' <span class="\' . $class . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</span>\';
}
else if ( $pagecount == 1 ) {
echo \' <a class="\' . $class . \'" href="\' . $base_url . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</a>\';
}
else {
echo \' <a class="\' . $class . \'" href="\' . $base_url . \'/page/\' . $pagecount . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</a>\';
}
endfor;
// if there is one more page after the current, print the next-link
if ( $catpage < $pages ) {
$nextpage = $catpage + 1;
echo \' <a class="next\' . $class . \'" href="\' . $base_url . \'/page/\' . $nextpage . \'"><i class="fa fa-angle-right"></i></a>\';
}
echo \'</div>\';
echo \'</nav>\';
printf( \'<span class="total-pages">\' . esc_html__( \'Page %1$s of %2$s\', \'codilight-lite\' ) . \'</span>\', $catpage, $pages );
echo \'</div>\';
endif;
// End of Pagination
?>
下面是包含上述分页代码的自定义类别模板:
<?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
$catpage = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
$catnum = 2;
$offset = ($catnum * $catpage) - 2;
$cat = get_category( get_query_var( \'cat\' ) );
$cat_id = $cat->cat_ID;
$child_categories=get_categories(
array(
\'parent\' => $cat_id,
\'orderby\' => \'id\',
\'order\' => \'DESC\',
\'hide_empty\' => \'0\',
\'number\' => $catnum,
\'offset\' => $offset,
\'paged\' => $catpage
)
);
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-custom.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; ?>
<?php
// Start of Pagination
$total_childs_query = get_categories( array( \'parent\' => $cat_id, \'hide_empty\' => \'0\' ));
$total_terms = count( $total_childs_query );
$pages = ceil($total_terms/$catnum);
$base_url = get_term_link( $cat_id, get_query_var( \'taxonomy\' ) );
// if there\'s more than one page
if( $pages > 1 ):
echo \'<div class="ft-paginate">\';
echo \'<nav class="navigation pagination" role="navigation">\';
echo \'<h2 class="screen-reader-text">Posts navigation</h2>\';
echo \'<div class="nav-links">\';
// if we\'re not on the first page, print the previous-link
if ( $catpage > 1 ) {
$prevpage = $catpage - 1;
if ( $prevpage > 1 ) {
echo \'<a class="prev page-numbers" href="\' . $base_url . \'/page/\' . $prevpage . \'"><i class="fa fa-angle-left"></i></a>\';
}
else {
echo \'<a class="prev page-numbers" href="\' . $base_url . \'"><i class="fa fa-angle-left"></i></a>\';
}
}
for ($pagecount=1; $pagecount <= $pages; $pagecount++):
//set class
$class = "page-numbers";
if ( $pagecount == $catpage ) {
$class .= " current";
}
if ( $pagecount == $catpage ) {
echo \' <span class="\' . $class . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</span>\';
}
else if ( $pagecount == 1 ) {
echo \' <a class="\' . $class . \'" href="\' . $base_url . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</a>\';
}
else {
echo \' <a class="\' . $class . \'" href="\' . $base_url . \'/page/\' . $pagecount . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</a>\';
}
endfor;
// if there is one more page after the current, print the next-link
if ( $catpage < $pages ) {
$nextpage = $catpage + 1;
echo \' <a class="next\' . $class . \'" href="\' . $base_url . \'/page/\' . $nextpage . \'"><i class="fa fa-angle-right"></i></a>\';
}
echo \'</div>\';
echo \'</nav>\';
printf( \'<span class="total-pages">\' . esc_html__( \'Page %1$s of %2$s\', \'codilight-lite\' ) . \'</span>\', $catpage, $pages );
echo \'</div>\';
endif;
// End of Pagination
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
最合适的回答,由SO网友:The J 整理而成
您需要至少传递2个参数,否则它将不知道要查找什么:
<?php
function custom_pagination($cat_id, $catnum) {
// Start of Pagination
$total_childs_query = get_categories( array( \'parent\' => $cat_id, \'hide_empty\' => \'0\' ));
$total_terms = count( $total_childs_query );
$pages = ceil($total_terms/$catnum);
$base_url = get_term_link( $cat_id, get_query_var( \'taxonomy\' ) );
// if there\'s more than one page
if( $pages > 1 ):
echo \'<div class="ft-paginate">\';
echo \'<nav class="navigation pagination" role="navigation">\';
echo \'<h2 class="screen-reader-text">Posts navigation</h2>\';
echo \'<div class="nav-links">\';
// if we\'re not on the first page, print the previous-link
if ( $catpage > 1 ) {
$prevpage = $catpage - 1;
if ( $prevpage > 1 ) {
echo \'<a class="prev page-numbers" href="\' . $base_url . \'/page/\' . $prevpage . \'"><i class="fa fa-angle-left"></i></a>\';
} else {
echo \'<a class="prev page-numbers" href="\' . $base_url . \'"><i class="fa fa-angle-left"></i></a>\';
}
}
for ($pagecount=1; $pagecount <= $pages; $pagecount++):
//set class
$class = "page-numbers";
if ( $pagecount == $catpage ) {
$class .= " current";
}
if ( $pagecount == $catpage ) {
echo \' <span class="\' . $class . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</span>\';
}
else if ( $pagecount == 1 ) {
echo \' <a class="\' . $class . \'" href="\' . $base_url . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</a>\';
} else {
echo \' <a class="\' . $class . \'" href="\' . $base_url . \'/page/\' . $pagecount . \'"><span class="screen-reader-text">Page</span>\' . $pagecount . \'</a>\';
}
endfor;
// if there is one more page after the current, print the next-link
if ( $catpage < $pages ) {
$nextpage = $catpage + 1;
echo \' <a class="next\' . $class . \'" href="\' . $base_url . \'/page/\' . $nextpage . \'"><i class="fa fa-angle-right"></i></a>\';
}
echo \'</div>\';
echo \'</nav>\';
printf( \'<span class="total-pages">\' . esc_html__( \'Page %1$s of %2$s\', \'codilight-lite\' ) . \'</span>\', $catpage, $pages );
echo \'</div>\';
endif;
// End of Pagination
}
?>
然后,在模板中,可以使用函数动态添加值:
$cat_id = 3;
$cat_num = 5;
custom_pagination($cat_id, $catnum);