0

I have a problem with pagination on search results page. The page links appear, but when i click onto page 2, it gives me a 404 error.

Page 1 : http://templategraphy.com/wp-demo/businessguru/?s=Blog+Image (page 1)

Page 2 : http://templategraphy.com/wp-demo/businessguru/page/2/?s=Blog+Image (Page not found)

search.php

<?php 

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    $args = array( 'paged' => $paged );
                    $the_query= new WP_Query($args);

                ?>
        <div>

         <?php  if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

        <div id="post-<?php the_ID(); ?>" <?php post_class('item'); ?>>

        <?php

       $featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );


       if ( has_post_thumbnail() ) {


        echo '<a class="single-image link-icon" href="'.get_the_permalink().'">';

       echo '<img src="'.$featuredImage.'" style="width:213px; height:110px;" />';

        echo '</a>';

       }

       ?>


      </div>


    <?php  endwhile; ?>


    <?php
      next_posts_link( 'Older Entries', $the_query->max_num_pages );
      previous_posts_link( 'Newer Entries' );
    ?>

   <?php 
    // clean up after our query
    wp_reset_postdata(); 
    ?>
    </div>

    <?php  else : ?>

   <h1 class="title">Not Found</h1>

   <p>Sorry, but you are looking for something that isn't here.</p>

   <?php  endif; ?>

  </div>

Please suggest some solutions.

  • Thanks @PieterGoosen for your suggestion but my problem is not solved i tried your code but its not working and please see my edited code and tell me where i am wrong.. – Payal Jul 2 '14 at 11:52
  • Suggest some solutions searching and pagination both are not working at the same time please see my code and suggest me what i am doing wrong in my code – Payal Jul 2 '14 at 12:14
1

okay sorry for mentioning only link as an answer.. if first pagination link shows 404 error that should be a permalink issue. but to u its second page so definitely it because of some code mistake. please check this code.

/*Loop Pagination - A WordPress script for creating paginated links on archive-type pages.*/

function loop_pagination( $args = array() ) {
global $wp_rewrite, $wp_query;

/* If there's not more than one page, return nothing. */
if ( 1 >= $wp_query->max_num_pages )
    return;

/* Get the current page. */
$current = ( get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1 );

/* Get the max number of pages. */
$max_num_pages = intval( $wp_query->max_num_pages );

/* Get the pagination base. */
$pagination_base = $wp_rewrite->pagination_base;

/* Set up some default arguments for the paginate_links() function. */
$defaults = array(
    'base'         => add_query_arg( 'paged', '%#%' ),
    'format'       => '',
    'total'        => $max_num_pages,
    'current'      => $current,
    'prev_next'    => true,
    //'prev_text'  => __( '&laquo; Previous' ), // This is the WordPress default.
    //'next_text'  => __( 'Next &raquo;' ), // This is the WordPress default.
    'show_all'     => false,
    'end_size'     => 1,
    'mid_size'     => 1,
    'add_fragment' => '',
    'type'         => 'plain',

    // Begin loop_pagination() arguments.
    'before'       => '<nav class="pagination loop-pagination">',
    'after'        => '</nav>',
    'echo'         => true,
);

/* Add the $base argument to the array if the user is using permalinks. */
if ( $wp_rewrite->using_permalinks() && !is_search() )
    $defaults['base'] = user_trailingslashit( trailingslashit( get_pagenum_link() ) . "{$pagination_base}/%#%" );

/* Allow developers to overwrite the arguments with a filter. */
$args = apply_filters( 'loop_pagination_args', $args );

/* Merge the arguments input with the defaults. */
$args = wp_parse_args( $args, $defaults );

/* Don't allow the user to set this to an array. */
if ( 'array' == $args['type'] )
    $args['type'] = 'plain';

/* Get the paginated links. */
$page_links = paginate_links( $args );

/* Remove 'page/1' from the entire output since it's not needed. */
$page_links = preg_replace( 
    array( 
        "#(href=['\"].*?){$pagination_base}/1(['\"])#",  // 'page/1'
        "#(href=['\"].*?){$pagination_base}/1/(['\"])#", // 'page/1/'
        "#(href=['\"].*?)\?paged=1(['\"])#",             // '?paged=1'
        "#(href=['\"].*?)&\#038;paged=1(['\"])#"         // '&#038;paged=1'
    ), 
    '$1$2', 
    $page_links 
);

/* Wrap the paginated links with the $before and $after elements. */
$page_links = $args['before'] . $page_links . $args['after'];

/* Allow devs to completely overwrite the output. */
$page_links = apply_filters( 'loop_pagination', $page_links );

/* Return the paginated links for use in themes. */
if ( $args['echo'] )
    echo $page_links;
else
    return $page_links;
 }
  • @payal : please mark it as the answer so that it will help others too – Zammuuz Jul 4 '14 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.