НЕ знаю куда и как установить переход по страницам (пагинацию)
Не знаю куда и как установить переход по страницам
Переводил archive-posts на простую страницу, все работает кроме перехода по страницам.
<?php
/*
* Template name:post-template
* */
get_header();?>
<!-- Content -->
<div class="Post" id="Post">
<h1><?php echo ale_get_option('postheader'); ?></h1>
<div class="wrapper flex">
<?php
$custom_query = new WP_Query( array('post_type' => 'posts'));
if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();?>
<div class="Post-content">
<a href="<?php the_permalink(); ?>">
<?php echo get_the_post_thumbnail($post->ID,'posts-mainsize');?>
<h6><?php the_title(); ?></h6>
</a>
<div class="desc-text">
<h4><?php the_title(); ?></h4>
<div><?php the_excerpt(); ?></div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<div class="select_post-page">
<?php ale_page_links(); ?>
</div>
</div>
<?php get_footer(); ?>
А пагинация и не будет работать на странице без внесения изменений...
Для начала хотелось бы увидеть код этой функции ale_page_links
function ale_page_links() { global $wp_query, $wp_rewrite; $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1; $pagination = array( 'base' => @add_query_arg('page','%#%'), 'format' => '', 'total' => $wp_query->max_num_pages, 'current' => $current, 'show_all' => false, 'type' => 'list', 'next_text' => 'Следующие посты', 'prev_text' => 'Предыдущие посты' ); if( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ); if( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) ); echo paginate_links($pagination); }Вот нашел еще такую функцию.
Но переходы не срабатывают.
function ale_page_links_custom($custom_query) { global $wp_query, $wp_rewrite; $custom_query->query_vars['paged'] > 1 ? $current = $custom_query->query_vars['paged'] : $current = 1; $pagination = array( 'base' => @add_query_arg('page','%#%'), 'format' => '', 'total' => $custom_query->max_num_pages, 'current' => $current, 'show_all' => true, 'type' => 'list', 'next_text' => 'Следующие посты', 'prev_text' => 'Предыдущие посты' ); if( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ); if( !empty($custom_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) ); echo paginate_links($pagination); }Для начала нужно добавить в запрос текущую страницу
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $custom_query = new WP_Query( [ 'post_type' => 'posts', 'paged' => $paged ] );Получается в конце функции дописать, с указанием названия страницы ?
<div class="wrapper flex"> <?php $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1; $custom_query = new WP_Query( [ 'post_type' => 'posts', 'paged' => $paged ] ); if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();?> <div class="Post-content"> <a href="<?php the_permalink(); ?>">это сделал
и какой результат?
если не работает можно взять эту функцию ale_page_links_custom($custom_query)
поидее должно работать.
или вообще сделать свою пагинацию (примерно так)
$max_page = $query->max_num_pages; $args = [ 'base' => str_replace( $max_page, '%#%', esc_url( get_pagenum_link( $max_page ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $max_page, ] echo paginate_links( $args );ну и пермалинки на всякий можно обновить
Вроде ссылка показывает что я на второй странице поста, но пост выводится первой.
<div class="Post" id="Post"> <h1><?php echo ale_get_option('postheader'); ?></h1> <div class="wrapper flex"> <?php $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1; $custom_query = new WP_Query(['post_type' => 'posts','paged' => $paged]); if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();?> <div class="Post-content"> <a href="<?php the_permalink(); ?>"> <?php echo get_the_post_thumbnail($post->ID,'posts-mainsize');?> <h6><?php the_title(); ?></h6> </a> <div class="desc-text"> <h4><?php the_title(); ?></h4> <div><?php the_excerpt(); ?></div> </div> </div> <?php endwhile;endif; ?> </div> <div class="select_post-page"> <?php ale_page_links_custom($custom_query); ?> </div> </div>ТО что я пробовал
проверил у себя - работает норм. если у вас не работает значит ошибка в другом месте... post_type кастомный? возможно где-то не хватает wp_reset_postdata();
проверил на archive-posts.php, не работает.
<?php get_header(); ?> <!-- Content --> <div class="Post" id="Post"> <h1><?php echo ale_get_option('postheader2'); ?></h1> <div class="wrapper flex"> <?php if (have_posts()) : while (have_posts()) : the_post();?> <div class="Post-content"> <a href="<?php the_permalink(); ?>"> <?php echo get_the_post_thumbnail($post->ID,'posts2-mainsize'); ?> <h6><?php the_title(); ?></h6> </a> <div class="desc-text"> <h4><?php the_title(); ?></h4> <div><?php the_excerpt(); ?></div> </div> </div> <?php endwhile; endif; ?> </div> <div class="select_post-page"> <?php ale_page_links(); ?> </div> </div> <?php get_footer(); ?>но эти две функции срабатывают
<span class="left"><?php echo get_previous_posts_link(__('< Newer Posts','aletheme')); ?></span> <span class="right"><?php echo get_next_posts_link(__('Older Posts >','aletheme')); ?></span>сам пост создаю так
function aletheme_get_post_types() { return array('posts' => array( 'config' => array( 'public' => true, 'menu_position' => 10, 'has_archive' => true, 'supports'=> array( 'title', 'editor', 'thumbnail', 'excerpt', ), 'show_in_nav_menus'=> true, ), 'singular' =>'Post first position', 'multiple' => 'Post first position', 'columns' => array( 'first_image', ) ), ); }Удаляя эти две строчки все работает, осуществляются переходы, как положено, а может что то не так, но этого я уже не знаю
base' => @add_query_arg('page','%#%'), 'format' => '',