Я не могу заставить нумерацию страниц работать с пользовательскими типами записей.
Он отображается правильно, когда я нажимаю, он переходит в / page / 2 и / page / 3 et c, и все эти выдает ошибку 404 Страница не найдена.
index. php
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('name1', 'name2', 'name3', 'post'),
'posts_per_page' => 4,
'post_status' => 'publish',
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php echo $paged; ?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
// do stuff here
<?php the_excerpt(); ?>
<?php endwhile; ?>
// get pagination
<?php mypagination( $query ); ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
функции. php
встроенный the_posts_pagination () ничего не выводит .
function mypagination($query){ ?>
<ul class="pagination">
<?php
$pages = paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => true,
'type' => 'array',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => '<span class ="fa fa-caret-left" aria-hidden="true"></span><span class="prev-text">Prev</span>',
'next_text' => '<span class="next-text">Next</span> <span class ="fa fa-caret-right" aria-hidden="true"></span>',
'add_args' => false,
'add_fragment' => '',
) );
if (is_array($pages)):
foreach ($pages as $p): ?>
<li class="pagination-item js-ajax-link-wrap">
<?php echo $p; ?>
</li>
<?php endforeach;
endif; ?>
</ul>
<?php
}
Если я создаю пользовательскую конечную точку API с тем же запросом / аргументами, разбиение на страницы работает.
/**
* Custom JSON API endpoint.
* Endpoint URL http://localhost/websitename/wp-json/frontpage/v1
*/
function api_frontpage_posts($data) {
$output = array();
// Get post slug.
$post = get_post($data['parent_id']);
$slug = $post->post_name;
// e.g. 1, 2, 3,...
$paged = $data['page_number'] ? $data['page_number'] : 1;
$query_args = array(
'post_type' => array('name1', 'name2', 'name3', 'post'),
'post_status' => array('publish'),
'posts_per_page' => 4,
'paged' => $paged,
);
// Create a new instance of WP_Query
$the_query = new WP_Query($query_args);
if (!$the_query->have_posts()) {
return null;
}
while ($the_query->have_posts()) {
$the_query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
$post_content = get_the_content();
$post_excerpt = get_the_excerpt();
$post_date = get_the_date('l, j F Y');
// Get the slug.
$post = get_post($post_id);
$post_slug = $post->post_name;
// Get image alt.
$alt = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true);
$desc = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_caption', true);
// Get image description.
$description = null;
$thumbnail = get_posts(array('p' => get_post_thumbnail_id(), 'post_type' => 'attachment'));
if ($thumbnail && isset($thumbnail[0])) {
$description = $thumbnail[0]->post_content;
}
// Image data.
$post_image = array(
'id' => get_post_thumbnail_id() ,
'url' => get_the_post_thumbnail_url(),
'caption' => get_the_post_thumbnail_caption(),
'alt' => $alt,
'description' => $description,
);
// Category data.
$categories = get_the_category($post_id);
$post_categories = array();
foreach ($categories as $key => $category) {
$post_categories[] = array(
'name' => $category->name,
'slug' => $category->slug,
'url' => get_category_link($category->cat_ID),
);
}
// Push the post data into the array.
$output[] = array(
'id' => "post" . $post_id, // cannot use numbers, or hyphens between words for Zurb data toggle attribute
'slug' => $post_slug,
'title' => $post_title,
'url' => get_permalink($post_id),
'date' => $post_date,
'content' => $post_content,
'excerpt' => $post_excerpt,
'image' => $post_image,
'categories' => $post_categories
);
}
$result = array(
"next" => (int)$paged === (int)$the_query->max_num_pages ? null : site_url() . '/' . $slug . '/page/' . ($paged + 1) . '/',
"prev" => (int) $paged === 1 ? null : site_url() . '/' . $slug . '/page/' . ($paged - 1) . '/',
// Split the array every 4 items.
"chunks" => array_chunk($output, 4)
);
// Reset the post to the original after loop. otherwise the current page
// becomes the last item from the while loop.
wp_reset_query();
// Return json.
return $result;
}
add_action('rest_api_init', function () {
register_rest_route('frontpage/v1', '/page/(?P<page_number>\d+)', array(
'methods' => 'GET',
'callback' => 'api_frontpage_posts',
));
});