user3615851 user3615851 - 1 year ago 71
Javascript Question

'Smart' history back button for Wordpress Custom Post Type

My Custom Post Type single post has a X button which uses

method to return to the archive page.

<span class="ion-android-close" onclick="javascript:history.back()"></span>

How could I achieve a back button which would remember the correct post anchor so the user would not have to scroll down again to the correct post when clicking back to the archive page?

Sorry for the lack of code, since I haven't found a good and easy solution to implement yet.

Answer Source

Considering your CPT archive should have an ID for each post like this :

<article id="post-1" ...>

You could just change your link in your CPT single template to this (this works only if there is no pagination used on the archive):

<a href="<?php echo get_post_type_archive_link('your-post-type') . '#post-' . get_the_ID(); ?>">