Previous post block does not work for all attachment pages
Description
If the template that is used to display attachments has a previous post block, the link does not always work.
- ✅ If the media is attached to a post or page, the link leads to that post/page.
- If the media is not attached, the link is visible but leads to the current page.
Note: The next post block never seems to be visible on the attachment page.
Step-by-step reproduction instructions
- Activate a full site editing theme that has an attachment template or any fallback template that has a previous post navigation block.
- Add a new item to the media library.
- From the media library, select the item and then "View attachment page"
- Confirm that the link is present and that the link leads to the current page.
Expected behaviour
For media that is not attached, I expect the link to lead to the previous attachment, or for the link to not be present.
Actual behaviour
The link leads to the current page.
WordPress information
- WordPress version: 5.7.1
- Gutenberg version: current trunk at https://github.com/WordPress/gutenberg/commit/959252b78687b329c3390e14cae397daa9e90247
- Are all plugins except Gutenberg deactivated? yes
- Are you using a default theme (e.g. Twenty Twenty-One)? No, Armando
Device information
- Device: Desktop
- Operating system: Windows 10
- Browser: Chrome Version 90.0.4430.93
Confirmed behavior of returning to the created post post when uploading images.
WordPress information
- WordPress version: 6.4.1 Gutenberg version: Version: 17.0.2
- Are all plugins except Gutenberg deactivated? yes
- Are you using a default theme (e.g. Twenty Twenty-One)? No, Armando
@justintadlock I know you like to use attachment pages, which behaviour would be the expected for the next / previous post links?
For media that is not attached, I expect the link to lead to the previous attachment, or for the link to not be present.
I was able to reproduce this issue on trunk (Gutenberg 24.2, WordPress 7.1.3, and Twenty Twenty-Five).
On an unattached attachment page, the Previous Post Navigation Link points back to the same attachment instead of displaying no link. Media attached to a post still links correctly to its parent post.
Root cause
The block calls get_previous_post_link(). When an attachment has no parent, post_parent is 0, and core's get_post( 0 ) falls back to the current global post. As a result, the Previous link points to the attachment being viewed.
Proposed approach
I'd suggest handling this case in the block by skipping the core link function when rendering a Previous link for an unattached attachment. The block would retain its existing empty wrapper, without the link or its associated styles.
I've tested this approach locally and added PHPUnit coverage for attached and unattached media. Normal posts and attachments with a parent remain unaffected.
A few things to consider
- Attachment pages are disabled by default on new installs since WordPress 6.4. They redirect to the media file unless enabled, so this mainly affects sites where attachment pages are enabled, including some upgraded sites.
- The underlying issue is in WordPress Core. A core fix could also address the same behaviour in classic themes. Would it make sense to open a Trac ticket for that, or is a Gutenberg-level fix preferred here?
- Expected behaviour: The issue mentions either hiding the link or navigating to the previous attachment. I'd lean towards hiding it for now, since navigating between attachments would introduce a different behaviour and additional questions about ordering.
- The Next link is a separate concern. Its current behaviour on attachment pages is unchanged and would remain outside the scope of this fix.
Drafted with the help of AI