2016-09-26 107 views
0

有沒有什麼辦法可以獲得category.php文件中的子類別而不是文章?如何獲取category.php中的子類別NOT帖子

我知道WordPress通過自己的查詢分頁category.php,我不應該創建一個新的查詢,但我需要僅顯示一些類別中的子類別而不是帖子。

而且,因爲我有很多,我還需要它來進行分頁 ...

回答

1

你能請嘗試以下代碼:

$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' => __('&laquo;'), 
    'next_text' => __('&raquo;'), 
    'total' => ceil($total/$numOfItems), 
    'current' => $page 
)); 

該代碼進行測試和工作完美。

+0

你爲什麼不先讀這個問題?如果可以的話,我會將其標記爲無用!這不會像我需要的那樣工作!如果我把它粘貼在category.php上,wordpress會計算髮布在其上的帖子的頁數,而不是子類別。我需要在某些類別中僅顯示子類別(不是職位)。 – Alex

+0

@Alex它顯示當前類別的子類別。 –

+0

你沒有讀到問題部分:**「而且,因爲我有很多,我也需要它分頁......」**!這沒有用! – Alex

相關問題