Getting post attchment URL to populate a CPT Admin Page Column

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 C

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

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

Share Improve this question asked May 21, 2020 at 11:20 Daniel FlorianDaniel Florian 591 gold badge1 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742410771a4438766.html

相关推荐

  • Getting post attchment URL to populate a CPT Admin Page Column

    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 C

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信