admin管理员组文章数量:1336584
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 badges1 Answer
Reset to default 1The 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 );
本文标签: Getting post attchment URL to populate a CPT Admin Page Column
版权声明:本文标题:Getting post attchment URL to populate a CPT Admin Page Column 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410770a2469740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论