Question

Just trying to work out how to get the url of a file attached to a Post and displaying that in the admin page for that CPT. The file is attached to the post via an ACF file upload field called 'grading_form'

I have the Admin Page columns setup (using my custom plugin) and the data is populated in all of the columns (again, using the custom plugin).

Each post has a PDF form file attached to it. I would like to have a column in the admin page which has a link to the PDF file.

So far I have the following:

function toogee_grading_applications_column( $column, $post_id ) {      
// Grading Form column
  if ( 'grading_form' === $column ) {
    $gradingForm = wp_get_attachment_url( 'grading_form' );

    if ( ! $gradingForm ) {
      _e( 'N/A' );  
    } else {
      echo $gradingForm;
    }
  }
}
add_action( 'manage_grading_applications_posts_custom_column', 'toogee_grading_applications_column', 10, 2);

Note: I have other columns that are all working using the above format but I just removed them from this in the interests of shortening this post

Once I have the URL of the PDF file attachment, I was just going to enter something like the following in the else statement to have a link in that column:

echo '<a href="'.$gradingForm.'">Grading Form</a>';

Unless there is a better way of doing it?

Thanks for any input

Était-ce utile?

La solution

The File field's documentation has some options you can try, but here's an example if your field's Return Value is array, where you can use $gradingForm['url'] to get the attachment URL:

$gradingForm = get_field( 'grading_form', $post_id );
echo $gradingForm ? '<a href="' . esc_url( $gradingForm['url'] ) . '">Grading Form</a>' : 'N/A';

And the proper way to use wp_get_attachment_url() is by passing the attachment (post) ID as the only parameter like this: wp_get_attachment_url( 123 ).

And in your case, here's an example without using get_field():

$att_id = get_post_meta( $post_id, 'grading_form', true );
$gradingForm_url = wp_get_attachment_url( $att_id );
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top