Support » Developing with WordPress » Can’t get previous_links working properly

  • Resolved halohtlo

    (@halohtlo)


    Hello mates,
    Basically I have a shopping cart with many products in different pages.
    When the user clicks on ” Add to Cart “, he/she gets redirected to the cart page -BUT- there I need a button ” Add and continue shopping” so the user can get redirected to the previous page and continue browsing through the products.

    I tried the following code but for some reason it redirects me to other page rather than the previous one.

    <?php	
    $prev_post = get_adjacent_post(false, '', true);
    if(!empty($prev_post)) {
    echo '<a href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>'; }
    ?>

    Thank you all for the help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • hi, can you try get_adjacent_post_link instead of get_adjacent_post + get_permalink?

    Also I think that for your situation the PHP referrer would be a better solution: $_SERVER['HTTP_REFERER'] 🙂

    Moderator bcworkz

    (@bcworkz)

    I agree with andraganescu that the HTTP referrer value will be better for your need. The adjacent post function takes one to the adjacent post in the default order of posts, not the last post the user was on.

    An alternative to using the referrer value would be to use JavaScript to essentially click the browser’s back button: window.history.back();

    Thank you for the feedback.
    I will try it tonight and let you know if it has worked.

    halohtlo

    (@halohtlo)

    Sorry for double posting, HTTP referrer worked out well.
    Thank you for the help and keep up the good work.
    Much appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Can’t get previous_links working properly’ is closed to new replies.