'Smart' history back button for Wordpress

2019-07-13 20:42发布

问题:

My Custom Post Type single post has a X button which uses history.back(); 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.

回答1:

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(); ?>">