WP Вывод пагинации для произвольного типа записи
Имеется произвольный тип записи news
задается в functions.php
add_action('init', function () {
$labels = array(
'name' => 'News',
'singular_name' => 'News',
'add_new' => 'Add News',
'add_new_item' => 'Add News',
'edit_item' => 'Corrected news',
'new_item' => 'New news',
'all_items' => 'All news',
'search_items' => 'Search news',
'not_found' => 'News in these types were not found',
'not_found_in_trash' => 'No such news were found in the basket',
'menu_name' => 'News'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => false,
'has_archive' => true,
'menu_icon' => 'dashicons-email-alt2',
'menu_position' => 5,
'supports' => array('title', 'editor', 'custom-fields', 'thumbnail'),
'taxonomies' => array('category', 'post_tag'),
'rewrite' => true,
'query_var' => true,
);
register_post_type('news', $args);
});
В нем задано 15 данных записей. Задача на странице page-news.php
вывести записи с пагинацией по 5 страниц.
Вот код подключения.
<div id="response" class="block_list">
<?php
$args = array(
'post_type' => 'news',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '5',
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) : $query->the_post();
$title = get_the_title();
echo '<div class="block_item">
<a class="block_item__link" href="' . get_permalink() . '">' . $title . '</a>
</div>';
endwhile; ?>
<div class="category_pagination">
<?php
$current_page = !empty( $_GET['news'] ) ? $_GET['news'] : 1;
echo paginate_links( array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'total' => $query->max_num_pages,
'current' => $current_page,
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __('Previous', 'proyob'),
'next_text' => __('Next', 'proyob'),
'class' => '',
) );
?>
</div>
<?php }
wp_reset_postdata(); ?>
<?php else : ?><p><?php esc_html_e('Missing posts with defined criteria', 'test'); ?></p><?php endif; ?>
</div>
Пагинация, отображает paginate_links(), но при нажатие на ссылку отображает не контент новой страницы, а index.php
.
Источник: Stack Overflow на русском