0

This shouldn't be so hard! But I cannot find the answer to this question on Google..

How can I link from an attachment template to the full post please?

Bonus question!

How can I grab the custom field data from the post that the attachment belong to on the attachment template?

I use this to grab the data on other template files:

<?php echo get_post_meta($post->ID, 'custom-field', true); ?>
2

If the attachment is attached to a post, the post is determined as "post_parent".

So, if you are in the attachement template (attachment.php, image.php, etc):

while ( have_posts() ) {
  the_post();
  // Get parent id of current post (get_the_ID() gets id of current post,
  // whicdh is the attachement, as we are in the attachment template)
  $parent_id = wp_get_post_parent_id( get_the_ID() );

}

You can get the parent id also with this method, but I just prefer the other:

while ( have_posts() ) {
  the_post();
  global $post;
  $parent_id = $post->post_parent;
}

Once you have the parent post ID, you can get the link, custom fields or whatever you want of the parent post:

  if( $parent_id ) {
      // parent post has been found, get its permalink
      $parent_url = get_permalink( $parent_id );

       ?>
       <a href="<?php echo $parent_url; ?>">
           <?php _e('Go to parent post', 'textdomain'); ?>
       <a/>
       <?php
       echo get_post_meta( $parent_id, 'custom-field', true );
  }
  • Thanks! Sorry for asking two questions at once. I used this code for the second question... – Algatella Feb 15 '16 at 10:35
  • 1
    What? I don't understand your comment "I used this code for the second question". This code fully answers your question. Both url of the parent post and cusotm fields of the parent post. To be honest, I think this is complete answer, not the other. – cybmeta Feb 15 '16 at 10:51
  • 1
    OK I agree with that. – Algatella Feb 15 '16 at 10:53
  • I've added more examples. – cybmeta Feb 15 '16 at 10:59
0

Q: How can I link from an attachment template to the full post please?

I think you are referring to something like this:

<a class="back-to-article" href="<?php echo get_permalink($post->post_parent); ?>">
  <?php _e('Get to Main Article', 'textdomain'); ?>
<a/>

Q: How can I grab the custom field data from the post that the attachment belong to on the attachment template?

Answering because it's your first question. From the next time, one question per se, please.

Your code was correct. You can use the code within a loop like:

echo get_post_meta(get_the_ID(), 'custom-field', true);

or you can use with global:

global $post;
echo get_post_meta($post->ID, 'custom-field', true);

Please check the whole code on the github repo for more idea.

  • Thanks, I used your answer to the first question and the other for the second. – Algatella Feb 15 '16 at 10:34

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.