I have a multifield file upload field for pdf,
I need to output file title name seen when uploading on media library, instead of the file path when I use the field on content template, tried to look around other support threads, could not get direct help.
What I need is to show file title and link it to pdf path ,
so when I click on file name displayed , it will take me to a path where the pdf will open on browser.
Please help on this!
I am stuck!
Regards!
Noman
Supporter
Languages:
English (English )
Timezone:
Asia/Karachi (GMT+05:00)
Hi Undule,
Thank you for contacting Toolset Support. We can add a custom shortcode to get the Uploaded file Title and Link.
1. Please add this code in your theme’s functions.php file:
add_shortcode( 'wpv_display_file_name', 'wpv_display_file_title_func');
function wpv_display_file_title_func($atts) {
global $wpdb;
$atts = shortcode_atts( array(
'url' => '',
'info' => '', // title, alt or id return
), $atts);
$res = '';
$url = $atts['url'];
$attachment_id = $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid = %s", $url
));
if($atts['info'] == 'title'){
$res = get_the_title($attachment_id);
}
if($atts['info'] == 'alt'){
$res = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
}
if($atts['info'] == 'id'){
$res = $attachment_id;
}
return $res;
}
2. Then you can use the following shortcode in your View or Content template:
<a href="[types field='resources' link='true' separator=', ' output='raw'][/types]" target="_blank" > [wpv_display_file_name info="title" url="[types field='resources' link='true' separator=', ' output='raw'][/types]"]</a>
==> Whereas types field='resources' is the name of your custom field (for the upload file).
Thank you