@jjmail

Почему не работает пагинация на странице тегов на сайте WordPress?

Привет, помогите пожалуйста не работает пагинация на странице тегов, на сайте wordpress. Выводится и появляется, но при переходе на 2 страницу ошибка 404.

Мой код для вывода записей в текущем теге:

<div class="background-type-2">
     <div class="main-content">
      <div class="line-ver">
       <div class="wrapper">
        <?php get_sidebar('blog'); ?>
        <div id="container">
           <div id="content" class="blog" role="main">
           <?php
           $current_page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
           $tag = get_queried_object();
           $args = array(
            'posts_per_page' => 7, 
            'tag' => $tag->slug,
            'paged'=> $current_page
            );
           query_posts($args);
           $wp_query->is_archive = true;
           $wp_query->is_home = false;
           while(have_posts()): the_post();
           ?>
           <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <div class="post_headline">
               <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
           </div>
    </article>
    <?php
    endwhile;
    if (function_exists('custom_pagination')) {
        custom_pagination($query->max_num_pages,"",$paged);
    }
    wp_reset_query();
    ?>
    </div><!-- #content -->

</div><!-- #container -->
</div>
</div>
</div>
</div>


Моя кастомная пагинация:

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

    if (empty($pagerange)) {
        $pagerange = 2;
    }

    /**
     * This first part of our function is a fallback
     * for custom pagination inside a regular loop that
     * uses the global $paged and global $wp_query variables.
     *
     * It's good because we can now override default pagination
     * in our theme, and use this function in default quries
     * and custom queries.
     */
    global $paged;
    if (empty($paged)) {
        $paged = 1;
    }
    if ($numpages == '') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
            $numpages = 1;
        }
    }

    /**
     * We construct the pagination arguments to enter into our paginate_links
     * function.
     */
    $pagination_args = array(
        'base'            => get_pagenum_link(1) . '%_%',
        'format'          => 'page/%#%',
        'total'           => $numpages,
        'current'         => $paged,
        'show_all'        => False,
        'end_size'        => 1,
        'mid_size'        => $pagerange,
        'prev_next'       => True,
        'prev_text'       => __('<i class="fa fa-angle-left fa-lg" aria-hidden="true"></i>'),
        'next_text'       => __('<i class="fa fa-angle-right fa-lg" aria-hidden="true"></i>'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
    );

    $paginate_links = paginate_links($pagination_args);

    if ($paginate_links) {
        echo "<nav class='custom-pagination'>";
        echo $paginate_links;
        echo "</nav>";
    }

}
  • Вопрос задан
  • 550 просмотров
Пригласить эксперта
Ответы на вопрос 2
@Nikelamoc
Вам надо использовать https://codex.wordpress.org/Plugin_API/Action_Refe... в functions.php с тегом https://codex.wordpress.org/Function_Reference/is_tag
И на странице обычный цикл будет работать как надо .
Ответ написан
Комментировать
@hofter
Здравствуйте. У меня была схожая проблема. Я смог ее решить, если еще актуально - ниже привожу код новой функции и ее применение в tag.php.

Функция в functions.php:
function my_new_pre_get_posts( $query ) {
       if ( is_tag() || $query->is_main_query() ) {
               $query->set( 'posts_per_page', '3' );
       return;
       }
}
add_action( 'pre_get_posts', 'my_new_pre_get_posts' );


tag.php:
<?php get_header(); ?>

<div class="container-fluid">
	<div class="body">
	<?php
		// global $post;
		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
		$related_tax = 'post_tag';
		$cats_tags_or_taxes = wp_get_object_terms( $post->ID, $related_tax, array( 'fields' => 'ids' ) );
		$args = array(
			'paged' => $paged,
			'tax_query' => array(
				array(
					'taxonomy' => $related_tax,
					'field' => 'id',
					'include_children' => false, 
					'terms' => $cats_tags_or_taxes,
					'operator' => 'IN'
				)
			)
		);

		$my_query = new WP_Query($args);
		if($my_query->have_posts() ) { ?>
			<h1>Записки із поміткою &#171;<?php single_tag_title(); ?>&#187;</h1>
		<?php
			while($my_query->have_posts() ) : $my_query->the_post(); ?>
				<div class="col-xs-12 col-sm-4 <?php promote_posts_add_class(); ?>">
					<a href="<?php the_permalink() ?>">
					    <div class="similiar-single">
					        <img class="similiar-single-image" src="<?php the_post_thumbnail('full'); ?>">
							<h1><?php the_title(); ?></h1>
					    </div>
					</a>
				</div>
			<?php endwhile;
		} else { ?>
			<div style="margin-top:70px" class="nothing-was-found-container">
				<div class="col-sm-5 hidden-xs">
					<div class="gears">
						<img class="first-gear" src="<?php bloginfo('template_url'); ?>/img/1.png">
						<img class="second-gear" src="<?php bloginfo('template_url'); ?>/img/2.png">
					</div>
				</div>
				<div class="col-xs-12 col-sm-7">
					<div class="text-message">
						<h3><?php error_text_message(); ?></h3>
					</div>
				</div>
			</div>
		<?php 
		}
		wp_reset_query();
	?>
		<div class="col-xs-12 col-sm-6 col-sm-offset-3">
			<?php wp_pagenavi(array( 'query' => $my_query)); ?>
		</div>
		<?php wp_reset_postdata(); ?>
	</div>
</div>

<?php get_footer(); ?>
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы