2

I have added pagination code(https://www.wpblog.com/use-wp_query-to-create-pagination/) im my website. This code works well in my page templates. But when i do the same coding in my custom taxonomy template it is not working from page 2. It goes to error page. Pleas help.

Code Here for taxonomy-somename.php

    <?php
/**
 * The template for displaying archive pages.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package Sydney
 */

get_header();
?>


    <div id="primary" class="content-area col-md-9">

        <main id="main" class="post-wrap" role="main">

<ul id="service-list">
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$paged = (get_query_var( 'page' )) ? get_query_var( 'page' ) : 1;
$aud_query = new WP_Query(
  array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    //'post_mime_type' => 'image',
    'post_mime_type' => 'audio',
    'posts_per_page' => 2,
    'paged'          => $paged,
    'post_parent__in' => $query->posts,
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'mymusic',
            'field'    => 'slug',
            'terms'    => $term->slug,
        ),
    ),
    )
);
?>
          <!----start-------->
<?php  if( $aud_query->have_posts() ) : ?>
<?php while($aud_query->have_posts()) :  $aud_query->the_post(); ?>
<?php echo
get_the_title($attach_id);
echo '<br>';
?>
<?php endwhile ?>
        <?php
        if (function_exists("pagination")) {
          pagination($aud_query->max_num_pages);
      }
        ?>           
        <?php else :?>
        <h3><?php _e('404 Error: Not Found', ''); ?></h3>

    <?php endif; wp_reset_postdata();?>


</ul><!-- #service-list -->
        </main><!-- #main -->
    </div><!-- #primary -->


<?php get_footer(); ?>
haripal
  • 81
  • 7

1 Answers1

6

Found the solution Myself from this link Wordpress - Custom Taxonomy Pagination Adding this code in functions.php and regenrate permalinks worked.

function taxonomy_rewrite_fix($wp_rewrite) {
    $r = array();
    foreach($wp_rewrite->rules as $k=>$v){
        $r[$k] = str_replace('catalog=$matches[1]&paged=','catalog=$matches[1]&page=',$v);
    }
    $wp_rewrite->rules = $r;
}
add_filter('generate_rewrite_rules', 'taxonomy_rewrite_fix');
haripal
  • 81
  • 7