admin管理员组

文章数量:1122826

So, I would like to have a back button on the single post page that can be dynamic to the point to where it knows where it is in the query and bring me back to the blog page on the right page number and category and search query string. For example -> "localhost/news-blogs/page/3/?s=news&nb-cat=16" Now I can do this wp_get_referer() If was to stay on that page, but if I click on the next post link then it would just bring me back to the last page since wp_get_referer() fn just hold the last page history and if I was to keep clicking next post link it would need to know to change the page #, so when I clicked back it would bring me back to the page with that post exists.

So, I would like to have a back button on the single post page that can be dynamic to the point to where it knows where it is in the query and bring me back to the blog page on the right page number and category and search query string. For example -> "localhost/news-blogs/page/3/?s=news&nb-cat=16" Now I can do this wp_get_referer() If was to stay on that page, but if I click on the next post link then it would just bring me back to the last page since wp_get_referer() fn just hold the last page history and if I was to keep clicking next post link it would need to know to change the page #, so when I clicked back it would bring me back to the page with that post exists.

Share Improve this question asked Sep 28, 2017 at 19:47 user1355485user1355485 1171 gold badge3 silver badges11 bronze badges 7
  • what is actually the question? what exactly do you have trouble with? – Mark Kaplun Commented Sep 28, 2017 at 20:01
  • I want a back button that has the correct URL that brings you back to that page you left off on. With the addition that if you were to navigate within the single post template that if you clicked the back button it would know that that post lives on page 6 or 2 or whatever, so that back button URL always brings you back to the exact place that blog post lives. Is that possible is there a WP function that provides that kinda help. Any help with direction and what I should be looking for would be much appreciated. – user1355485 Commented Sep 28, 2017 at 20:19
  • sorry, will make it more explicitm what code have you written, what are the exact problems you have with your code, or what wordpress concept you would like to be clarified? – Mark Kaplun Commented Sep 28, 2017 at 20:39
  • The code I wrote. I removed because I realized that it wouldn't keep track of the correct page # based on where it is on the blog. What I did write was I grabbed all the query strings and created a query string of the page id, so I could inject it into the URL back button link. Which works, but then I realized it has one downfall which is that the code can't keep track of the correct page id that post lives on. – user1355485 Commented Sep 28, 2017 at 20:49
  • It looked like this expect I grab the page id as well <a href="/news-blogs/<?php if($_GET['nb-cat'] || $_GET['s']) echo '?'; if($_GET['s']) echo 's=' . $_GET['s']; if($_GET['nb-cat'] && $_GET['s']) echo '&'; if($_GET['nb-cat']) echo 'nb-cat=' . $_GET['nb-cat']; ?>" class="<?php echo $classes; ?>"> – user1355485 Commented Sep 28, 2017 at 20:50
 |  Show 2 more comments

1 Answer 1

Reset to default 1

So it looks like you can't get the page number unless you query the whole post type and query strings and then check that against how many pages you show on each page and then do some math to find what page that will be on, so I stuck with the next best thing. I kept the page number and echoed it along the post url and next and prev post links, so then I grab the number in the query string and echo it out on the back button.

// Get page number and set it

$getPageNumber = (get_query_var('paged')) ? get_query_var('paged') : 1;
$_GET['number'] = $getPageNumber;

// Post link

<?php
  the_permalink();
  if($_GET['nb-cat'] || $_GET['s']) echo '?';
  if($_GET['s']) echo 's=' . $_GET['s'];
  if($_GET['nb-cat'] && $_GET['s']) echo '&';
  if($_GET['nb-cat']) echo 'nb-cat=' . $_GET['nb-cat'];
  if(!isset($_GET['nb-cat']) && !isset($_GET['s'])) echo '?';
  if(isset($_GET['nb-cat']) || isset($_GET['s'])) echo '&';
  if($_GET['number']) echo 'number=' . $_GET['number'];
  ?>

// Back btn Link

<a href="/news-blogs/
 <?php
    if($_GET['number']) echo 'page/' . $_GET['number'];
    if($_GET['nb-cat'] || $_GET['s']) echo '?';
    if($_GET['s']) echo 's=' . $_GET['s'];
    if($_GET['nb-cat'] && $_GET['s']) echo '&';
    if($_GET['nb-cat']) echo 'nb-cat=' . $_GET['nb-cat'];
  ?> 
<a/>

// Next and Prev post Links

<?php if ( $prev != get_permalink() ) : ?>
  <a href="<?php echo $prev; if($_GET['number']) echo '?number=' . $_GET['number']; ?>" class="prev btn border padding">Prev</a>
<?php endif; ?>

<?php if ( $next != get_permalink() ) : ?>
  <a href="<?php echo $next; if($_GET['number']) echo '?number=' . $_GET['number']; ?>" class="next btn border padding">Next</a>
<?php endif; } ?>

本文标签: