Pagination on static front page wp not working [Resolved]. Пейджинация на статичной главной странице вп

Итак, на фронтальной странице при выводе кастомного типа постов, пейджинация, которая работает на других шаблонах страниц, не работает. Способы вывода представлены на ВП-кодексе, среди них и функция вывода пейджинации на статичной главной странице:

**

Static front page pagination for custom posts on wordpress doesn’t work even if the same code works on other page-templates, showing custom posts. Wp-codex says that the code for static front page differs, here is the link:

https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

Привожу код (Code for WP_Query of custom post type):

/* for pagination */
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
$posts_per_page = get_option('posts_per_page');
$offset = ( $paged - 1 ) * $posts_per_page;
$args = array(
'post_type' => 'custom_post_type_slug',
'posts_per_page' => $post_per_page,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'offset'=> $offset,
);
$posts = new WP_Query($args);
$max_pages = $posts->max_num_pages;
while ( $posts->have_posts() ) : $posts->the_post();

Вывод пейджинации (code for pagination):

if($max_pages > 1){
$current_page = max(1, get_query_var('page'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $max_pages,
));

Кстати, в вп-кодексе написано, что квери нужно обязательно на главной странице запрашивать ф-цией WP_Query(), а не query_posts().

В чем часто проблема — пейджинация работает (по запросу mysite.com/page/2/ например), но ссылки «Вперед» и «Назад» не обновляют параметры урл, то есть, к примеру ссылка вида a href=»https://mysite.com/hotel/page/3″ под лейблом «Вперед» после попадания на третью страницу не обновляется до вида href=»https://mysite.com/hotel/page/4″. Или находишься на любой странице, а ссылка под цифрой «1» (на первую страницу поиска) не становится активной.

Например, нерешенный вопрос на Stack Overflow: https://wordpress.stackexchange.com/questions/62529/pagination-is-somewhat-working-on-frontpage-doesnt-update-when-prev-is-click — у автора почти корректный код, но пейджинация не работает.

**

Sometimes pagination works when you force it by changing URL (mysite.com/page/2/ for example), but Next and Back links do not update themselves. For example the link that looks like a href=»https://mysite.com/hotel/page/3″ for «Next» button doesn’t become a href=»https://mysite.com/hotel/page/4″ after you come to page 3. Or you come on page 2, but the button labeled as «1» doesn’t become active.

For example this wasn’t resolved on Stack Overflow: https://wordpress.stackexchange.com/questions/62529/pagination-is-somewhat-working-on-frontpage-doesnt-update-when-prev-is-click. The code of the author of the question is almost correct, but he had to change just 1 word:

Нужно было поменять только 1 слово:

И видимо — это не всегда так. Нужно смотреть, какой формат ссылок конкретно у вас в строке браузера. Если у меня формат mysite.com/page/2/, то и get_query_var должен быть page, а не paged.

**

May be it’s not always like that because you should check the url of your working pagination and find out if it’s PAGE or PAGED. The function from WP Codex does that this way:

И самый верхний код, который я тут привожу — он как раз проверяет формат ссылок:

if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }