我的项目需要在一个页面中列出所有类别的分页。
示例:我有100个类别,需要为每个页面显示10个类别,并在下面添加数字分页。
我怎么能在Wordpress中做到这一点?
$taxonomy = 'category';
if( !isset($_GET['showall']) ):
$total_terms = wp_count_terms( 'category' );
$pages = ceil($total_terms/$per_page);
// if there's more than one page
if( $pages > 1 ):
echo '<ul>';
for ($pagecount = 1; $pagecount <= $pages; $pagecount++):
echo '<li><a href="'.get_term_link('news', $taxonomy).'page/'.$pagecount.'/">'.$pagecount.'</a></li>';
endfor;
echo '</ul>';
endif;
else:
endif;
它返回域名/类别/新闻/页面/号码,但我点击页码返回主页。
答案 0 :(得分:0)
最后我是靠自己做的...... 这是代码,如果你愿意,可以随意使用。
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$category_per_page = 2;
$page = 1;
$offset = 0;
$taxonomy = 'category';
if (!empty($_GET['page'])){
$offset = ($_GET['page'] - 1) * $category_per_page;
}
$tax_terms = get_terms( $taxonomy, array( 'parent' => $cat_id,'number' => $category_per_page, 'offset' => $offset , 'hide_empty' => false ) );
$totalCategories = get_terms( $taxonomy, array( 'parent' => $cat_id, 'hide_empty' => false ) );
$totalNumber = sizeof($totalCategories);
$totalPagination = round($totalNumber / $category_per_page);
foreach ($tax_terms as $tax_term) :
$args=array('cat' => $tax_term->term_id,'showposts'=>6);
$query=new WP_Query($args);
?>
<div class="col-xs-12">
<div class="heading">
<h3 class="heading-title"><a href="<?=esc_attr(get_term_link($tax_term, $taxonomy));?>" title="<?=the_title();?>"><?= $tax_term->name;?></a></h3>
</div>
<section class="list-items owl-carousel owl-theme owl-loaded owl-drag" id="projects-slide">
<?php while($query->have_posts()):$query->the_post();?>
<div class="item">
<figure class="item-featured-img">
<a href="<?=the_permalink();?>">
<?php if ( has_post_thumbnail() ) :
the_post_thumbnail('thumbnail',array('class' => 'img-responsive center-block'));
else : ?>
<img class="img-responsive center-block" src="<?php bloginfo('template_url');?>/img/no-img.jpg" alt="<?php echo $alt_text; ?>">
<?php endif ?>
</a>
<figcaption class="title-item">
<h2><a href="<?=the_permalink();?>" title="<?=the_title();?>"><?=the_title();?></a></h2>
</figcaption>
</figure>
<div class="excerpt">
<p><?php echo cut_string(get_the_content(),65,'');?></p>
</div>
</div>
<?php endwhile;?>
</section>
</div>
<?php endforeach;?>
<form method="GET">
<?php for ($i=0; $i < $totalPagination ; $i++) { ?>
<button name="page" value="<?= $i+1 ?>"><?= $i+1 ?></button>
<?php } ?>
</form>