I want to show a link on the attachment page which takes one back to it's parent post. Currently in my theme I do not have that option, the links I get on the attachment page are Previous/Next links. Please note I don't have any knowledge of php. I was wondering is there anyway if anyone can help me achieve this.

I would really appreciate the help.


Here's one way within the main loop on the attachment's page:

First we get the parent ID from the current attachment's post:

$parent_id = get_post_field( 'post_parent' );

Then we check if it's non-zero and print the link to the parent:

if ( $parent_id ) {
        '<a href="%1$s">%2$s</a>',
        esc_url( get_permalink( $parent_id ) ),
        esc_html__( 'Go to parent', 'wpse' )

Hope you can expand this to your needs!

| improve this answer | |
  • 1
    It works exactly like I wanted. Thanks a ton! I really appreciate it! – John D Aug 25 '18 at 15:32
  • Glad to hear it worked for you – birgire Aug 25 '18 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.