如何获取category.php中的子类别NOT帖子
问题描述:
有没有什么办法可以获得category.php
文件中的子类别而不是文章?如何获取category.php中的子类别NOT帖子
我知道WordPress通过自己的查询分页category.php
,我不应该创建一个新的查询,但我需要仅显示一些类别中的子类别而不是帖子。
而且,因为我有很多,我还需要它来进行分页 ...
答
你能请尝试以下代码:
$args = array('child_of' => get_query_var('cat'));
$categories = get_categories($args);
$numOfItems = 2; // Set no of category per page
$page = isset($_GET['cpage']) ? abs((int) $_GET['cpage']) : 1;
$to = $page * $numOfItems;
$current = $to - $numOfItems;
$total = sizeof($categories);
echo '<ul class="content">';
for($i=$current; $i<$to; ++$i) {
$category = $categories[$i];
if($category->name) {
echo '<li>Category: <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a></li>';
}
}
echo '</ul>';
unset($category);
/* For pagination */
echo paginate_links(array(
'base' => add_query_arg('cpage', '%#%'),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total/$numOfItems),
'current' => $page
));
该代码进行测试和工作完美。
你为什么不先读这个问题?如果可以的话,我会将其标记为无用!这不会像我需要的那样工作!如果我把它粘贴在category.php上,wordpress会计算发布在其上的帖子的页数,而不是子类别。我需要在某些类别中仅显示子类别(不是职位)。 – Alex
@Alex它显示当前类别的子类别。 –
你没有读到问题部分:**“而且,因为我有很多,我也需要它分页......”**!这没有用! – Alex