• Resolved juri003

    (@juri003)


    Hi everyone.

    I am struggling with a paged list of posts.

    The problem is that whenever you click an item, the pagination resets to first page. I’ve been searching quite some time but I can’t find a working solution.

    This is the query I used:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    'post_type' => page,
    	'post_parent'  => $post->post_parent,
    	'posts_per_page' => 12,
    	'orderby' => menu_order,
    	'paged'=>$paged,
    	'order' => ASC
    );
    query_posts($args);

    You can see the output here: https://2010.jurisverbeelding.nl/foto/portret

    Thanks in advance!

Viewing 10 replies - 1 through 10 (of 10 total)
  • If you are looking for a way to return to the previous page, like the browser ‘Back’ button, maybe one of the ‘breadcrumb’ plugins will work. Do a google search on ‘wordpress breadcrumbs’.

    Thread Starter juri003

    (@juri003)

    Hey vtxyzzy,

    Thanks but that is not what I am looking for.

    I have long a list of posts that is paginated (splitted over several pages) because else it would be to long.

    Whenever you click on one of the posts, the list returns to the first page instead of keeping the current page position.

    In the example you can see what I mean, the post-list consits of thumbs.

    https://2010.jurisverbeelding.nl/foto/portret

    I understand now. I can’t be sure, but I think this will work.

    I assume you have a loop to display the thumbnails and it uses the ‘paged’ variable like this:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'paged' => $paged,
       ... more args ...
    );
    query_posts($args);

    You need to do two things:

    1. Create a new currpage URL parameter and check for it before the query.
    2. Add the variable to the URL of each thumbnail.
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $paged = (isset($_GET['currpage'])) ? $_GET['currpage'] : $paged;
    $args = array(
       'paged' => $paged,
       ... more args ...
    );
    query_posts($args);

    Then, add to each thumbnail URL ‘?currpage=$paged’.

    Thread Starter juri003

    (@juri003)

    Hey vtxyzzy,

    Thanks again! This looks like the solution but I have no clue how to add the variable to the url. I’ve Been searching for examples and trying some but no.

    This is the whole output:

    <li <?php if (is_page($post->ID)) { echo'class="current_page_item"'; } ?> id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a></li>

    Can you explain how to add the ‘?currpage=$paged’ variable?

    You will need a function to add a parameter to a link. Add this to the end of your functions.php:

    <?php function add_parameter_to_link ($link,$key,$value) {
       // Adds the parameter $key=$value to $link, or replaces it if already there.
       if (strpos($link,'href')) {
         $hrefpat = '/(href *= *([\"\']?)([^\"\' ]+)\2)/';
       } else {
         $hrefpat = '/(([\"\']?)(http([^\"\' ]+))\2)/';
       }
       if (preg_match($hrefpat,$link,$matches)) {
          $url = $matches[3];
          $newurl = $url;
          // Now see if there are already parameters in the URL
          if (strpos($newurl,'/?')) {
             // If $key is already there, just update, otherwise, add it.
             if (preg_match("/$key=[^&]+/",$newurl)) {
                $newurl = preg_replace("/$key=[^&]+/","$key=$value",$newurl);
             } else {
                $newurl .= "&$key=$value";
             }
          } else {
             // No parameters, just add ?$key=$value
             $newurl = trailingslashit($newurl) . "?$key=$value";
          }
          // echo '<p>OLDURL:' . htmlspecialchars($url) . '</p>';
          // echo '<p>NEWURL:' . htmlspecialchars($newurl) . '</p>';
          $link = str_replace($url,$newurl,$link);
       }
    
       return $link;
    }
    ?>

    Then, use this to create your links:

    <li <?php if (is_page($post->ID)) { echo'class="current_page_item"'; } ?> id="post-<?php the_ID(); ?>"><a href="<?php echo add_parameter_to_link(get_the_permalink(),'currpage',$paged); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a></li>

    Thread Starter juri003

    (@juri003)

    Hey vtxyzzy,

    Again, thanks for your help, I really appreciate it!

    There is a bug somewhere that stops the script and pagebuild. The page ends after this:

    <li id="post-175"><a href="

    You can see it here live: https://2010.jurisverbeelding.nl/foto/portret/siren-dreams

    I really try to gain knowledge on these things but it is not sufficient yet to fix it.

    cheers

    OK, forget the add_parameter_to_link function. I found a built-in function that does that job. Please try this to create your links:

    <li <?php if (is_page($post->ID)) { echo 'class="current_page_item"'; } ?> id="post-<?php the_ID(); ?>"><a href="<?php echo add_query_arg('currpage',$paged,get_permalink()); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a></li>

    Thread Starter juri003

    (@juri003)

    Hey vtxyzzy,

    You really rock!

    It works now but another problem comes with the solution. The previous_posts_link & next_posts_link don’t work anymore after clicking on a item.

    If it is possible to add for example /page/2 to the link instead of ?page=2 The problem would be solved

    I allready tried 2 other options without succes:

    1. change the parameter into ‘page’ instead of ‘currpage’
    2. put a remove_query_arg on the previous_post_link

    Did you add this code at the top of the loop?

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $paged = (isset($_GET['currpage'])) ? $_GET['currpage'] : $paged;
    $args = array(
       'paged' => $paged,
       ... more args ...
    );
    query_posts($args);

    If so, what does previous/next_posts_link generate?

    Thread Starter juri003

    (@juri003)

    SOLVED!

    I replaced the argument ‘currpage’ into ‘paged’ and removed the parameter before the query: $paged = (isset($_GET['currpage'])) ? $_GET['currpage'] : $paged;

    Another thanks to vtxyzzy!

    Here’s the whole deal:

    <div class="postnavcont">
               <ul>
       			<?php
    			 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args = array(
                    'post_type' => page,
    				'post_parent' => $post->ID,
    				'posts_per_page' => 12,
    			 	'orderby' => menu_order,
    				'paged'=>$paged,
    				'order' => ASC
               );
                query_posts($args);
    
    if ( has_post_thumbnail() ) {
    
    	} else {
    
    }
                 while (have_posts()) : the_post(); ?>
    
    <li <?php if (is_page($post->ID)) { echo 'class="current_page_item"'; } ?> id="post-<?php the_ID(); ?>"><a href="<?php echo add_query_arg('paged',$paged,get_permalink()); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a></li>
    		<?php endwhile; ?>
    
          		  <li class="butprev"><?php previous_posts_link('<img src="' . get_bloginfo(stylesheet_directory) . '/images/spacer_35.gif" width="35" height="35" alt="Volgende" />') ?></li>
                  <li class="butnext"><?php next_posts_link('<img src="' . get_bloginfo(stylesheet_directory) . '/images/spacer_35.gif" width="35" height="35" alt="Vorige" />') ?></li>
    
                    </ul>
                </div>
            <?php wp_reset_query(); ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘paged list with current position’ is closed to new replies.