Как добавить пагинацию рубрик записей в WordPress
В этой статье мы рассмотрим, как добавить пагинацию рубрик записей в ВордПресс. Для этого мы будем использовать код, который будет размещен в файле function.php.
Шаг 1: Создайте объект для сортировки рубрик
Первым шагом является создание объекта для сортировки рубрик. Мы будем использовать класс wpse_99513_adjacent_category, который мы поместим в файл function.php.
class wpse_99513_adjacent_category {
public $sorted_taxonomies;
/**
* @param string Taxonomy name. Defaults to ‘category’.
* @param string Sort key. Defaults to ‘id’.
* @param boolean Whether to show empty (no posts) taxonomies.
*/
public function __construct( $taxonomy = ‘category’, $order_by = ‘id’, $skip_empty = true ) {
$this->sorted_taxonomies = get_terms(
$taxonomy,
array(
‘get’ => $skip_empty ? » : ‘all’,
‘fields’ => ‘ids’,
‘hierarchical’ => false,
‘order’ => ‘ASC’,
‘orderby’ => $order_by,
)
);
}
/**
* @param int Taxonomy ID.
* @return int|bool Next taxonomy ID or false if this ID is last one. False if this ID is not in the list.
*/
public function next( $taxonomy_id ) {
$current_index = array_search( $taxonomy_id, $this->sorted_taxonomies );
if ( false !== $current_index && isset( $this->sorted_taxonomies[ $current_index + 1 ] ) )
return $this->sorted_taxonomies[ $current_index + 1 ];
return false;
}
/**
* @param int Taxonomy ID.
* @return int|bool Previous taxonomy ID or false if this ID is last one. False if this ID is not in the list.
*/
public function previous( $taxonomy_id ) {
$current_index = array_search( $taxonomy_id, $this->sorted_taxonomies );
if ( false !== $current_index && isset( $this->sorted_taxonomies[ $current_index — 1 ] ) )
return $this->sorted_taxonomies[ $current_index — 1 ];
return false;
}
}
Шаг 2: Использование объекта в файле archive.php
После создания объекта для сортировки рубрик, мы будем использовать его в файле archive.php, который отображает список записей для определенной рубрики. Мы будем использовать следующий код для вывода ссылок на предыдущую и следующую рубрики:
class wpse_99513_adjacent_category {
public $sorted_taxonomies;
/**
* @param string Taxonomy name. Defaults to ‘category’.
* @param string Sort key. Defaults to ‘id’.
* @param boolean Whether to show empty (no posts) taxonomies.
*/
public function __construct( $taxonomy = ‘category’, $order_by = ‘id’, $skip_empty = true ) {
$this->sorted_taxonomies = get_terms(
$taxonomy,
array(
‘get’ => $skip_empty ? » : ‘all’,
‘fields’ => ‘ids’,
‘hierarchical’ => false,
‘order’ => ‘ASC’,
‘orderby’ => $order_by,
)
);
}
/**
* @param int Taxonomy ID.
* @return int|bool Next taxonomy ID or false if this ID is last one. False if this ID is not in the list.
*/
public function next( $taxonomy_id ) {
$current_index = array_search( $taxonomy_id, $this->sorted_taxonomies );
if ( false !== $current_index && isset( $this->sorted_taxonomies[ $current_index + 1 ] ) )
return $this->sorted_taxonomies[ $current_index + 1 ];
return false;
}
/**
* @param int Taxonomy ID.
* @return int|bool Previous taxonomy ID or false if this ID is last one. False if this ID is not in the list.
*/
public function previous( $taxonomy_id ) {
$current_index = array_search( $taxonomy_id, $this->sorted_taxonomies );
if ( false !== $current_index && isset( $this->sorted_taxonomies[ $current_index — 1 ] ) )
return $this->sorted_taxonomies[ $current_index — 1 ];
return false;
}
}