Home › Toolset Professional Support › [Resolved] Displaying File Title with a Link for Custom Field for Manuals, etc
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Karachi (GMT+05:00)
Related documentation:
This topic contains 7 replies, has 2 voices.
Last updated by Waqar 4 years, 6 months ago.
Assisted by: Waqar.
Tell us what you are trying to do?
I'm using Custom Fields to add in the ability for my client to add product brochures to their WooCommerce webstore (and other such downloads). I'm currently trying to output the file title that links to the file URL. All the instructions I've found either don't work or only output the end section of the URL (so if the URL is mywebsite.com/uploads/myfile.pdf it will show myfile.pdf, instead of the meta title of My File Brochure v1 etc).
Is there any documentation that you are following?
Numerous links on here
https://toolset.com/forums/topic/display-file-name-of-uploaded-file-from-user-field/
https://toolset.com/forums/topic/displaying-file-name-title-uploaded-from-admin/
https://toolset.com/forums/topic/display-file-name-of-uploaded-file/#post-575699
https://toolset.com/forums/topic/print-out-file-name-in-view-template/#post-17264
Is there a similar example that we can see?
I think it's fairly self-explanatory, buy you can see the sort of thing I'm trying to achieve on the actual website here hidden link and at the bottom, clicking into Downloads and you will see the list of files. Essentially, the system being used is very clunky compared to the ease of adding custom fields and files in with Types, but getting Types to output a File Title rather than just a raw URL is hard work and something I really would have expected to have been a tick box in the code.
What is the link to your site?
hidden link is the page I've been testing on.
Hi,
Thank you for contacting us and I'd be happy to assist.
To get the meta title of a file uploaded to the media library, you'll need a custom shortcode that uses "attachment_url_to_postid" function to first get the attachment ID of a file whose URL is provided and then returns the title of that attachment.
( ref: https://developer.wordpress.org/reference/functions/attachment_url_to_postid/ )
Example:
add_shortcode( 'get-file-title', 'get_file_title_func'); function get_file_title_func($atts) { $url = $atts['url']; $attachment_id = attachment_url_to_postid( $url ); if($attachment_id > 0) { $attachment_title = get_the_title($attachment_id); } if ($attachment_title) { return $attachment_title; } }
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
After that, you'll be able to use this shortcode to get the attachment title by providing the file's URL like this:
Case 1: When a file type custom field with slug "file-custom-field" can only have one instance or value:
<a href="[types field='file-custom-field' output='raw'][/types]" target="_self"> [get-file-title url="[types field='file-custom-field' output='raw'][/types]"] </a>
Case 2: When a file type custom field with slug "file-custom-field" can have multiple instances or values:
[wpv-for-each field="wpcf-file-custom-field"] <li> <a href="[wpv-post-field name='wpcf-file-custom-field']" target="_self"> [get-file-title url="[wpv-post-field name='wpcf-file-custom-field']"] </a> </li> [/wpv-for-each]
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi
Thanks for your response, however, this isn't working for me.
I'm not using Views to create the page layouts, but using the Divi Theme Builder instead as I can get more easily the look they want to go for across all the proucts.
What I want to do is use the Custom Fields option for ToolSet within a short (that will display in Divi) to produce this.
I've tried using your method above, and this just spits out some of the code and none of the information relative to the file. I've even tried putting this within a code module to be sure.
Is there a way of creating a small view that I can then call as that might work?
Otherwise, the closest I have gotten to this is from here https://toolset.com/forums/topic/print-out-file-name-in-view-template/#post-17264 where it outputs the filename and not the whole URL, but I really want the title to be the output.
I've also tried to make sense of what Short Code and argument I should use this the resolve from this ticket https://toolset.com/forums/topic/display-file-name-of-uploaded-file/#post-575699 as that sounds like it would be the thing I want to do too.
The other factor in this is that some of the fields will have more than a single document in them (so there might be brochures in English, Greek, Spanish etc).
I look forward to your response
Andy 🙂
Further information.
So on the page hidden link I have used a view shortcode [wpv-view name="Display Brochures"]
I have added in Start and End notes so you can see what is output from the view.
All the bullet points are output when I tried to use the shortcode information you gave me, then right at the bottom, you can see the 2 files links from the custom field.
In my loop for this I have:
[wpv-layout-start] [wpv-items-found] <!-- wpv-loop-start --> <ul class="wpv-loop js-wpv-loop"> <wpv-loop> <li>[wpv-post-body view_template="loop-item-in-display-brochures"]</li> </wpv-loop> </ul> <!-- wpv-loop-end --> [/wpv-items-found] [wpv-no-items-found] <strong>[wpml-string context="wpv-views"]No brochure found[/wpml-string]</strong> [/wpv-no-items-found] [wpv-layout-end]
and presently in the Loop Item, I have
<a href="[types field='product-brochure' output='raw'][/types]" target="_self" rel="noopener noreferrer"> [get-file-title url="[types field='product-brochure' output='raw'][/types]"] </a> [types field="product-brochure"][/types]
then in the output I have
[wpv-filter-meta-html] [wpv-layout-meta-html]
I really would like to get this working for the product-brochure, then I can easily apply this to the other fields I need to completed.
I look forward to. your response.
Thanks
Andy
Ok, I'm getting closer to what I want with a few combinations of things.
So I've taken the code from here >> https://toolset.com/forums/topic/file-post-field-displaying-file-title-text-rather-than-file-path/
I've put this in place, and I'm not seeing the File Titles like I wanted.
So I took this a stage further, and I've manged to half get the links working with the following:
Loop Editor:
[wpv-layout-start] [wpv-items-found] <!-- wpv-loop-start --> <wpv-loop> [wpv-post-body view_template="loop-item-in-display-brochures"] </wpv-loop> <!-- wpv-loop-end --> [/wpv-items-found] [wpv-no-items-found] <strong>[wpml-string context="wpv-views"]No brochure found[/wpml-string]</strong> [/wpv-no-items-found] [wpv-layout-end]
Loop Item in Display:
<a href="[types field='product-brochure' output='raw' item='$current_page'][/types]" title="[my_file_name types_field="product-brochure"]" target="_blank">[my_file_name types_field="product-brochure"]</a>
Output Editor:
[wpv-filter-meta-html] [wpv-layout-meta-html]
You can see now how this is appearing on the website at >> hidden link
The main problem I have now is that even with the $current_page element, that I'm still seeing documents from ALL products that have one added, and that the products from each are getting their URLS all messed up so they don't actually work, plus I get a whole list of bullets, which I suspect is related to the fact it's showing everything from everywhere and not just the documents from the page the view is on.
This really shouldn't be as difficult as it is to implement. It's a really basic thing and having looked around the forums, I can see so many people who have asked for the same thing for a good number of years.
Please help me get this sorted ASAP
Thanks
Andy
Scrap that, I think I've solved it.
Taking the code from https://toolset.com/forums/topic/file-post-field-displaying-file-title-text-rather-than-file-path/, I've doctored the shortcode function to basically inject the URL from the files as well, and that now displays as I want it to.
For anyone looking, copy the following into your functions.php file (or I guess the Toolset side of things but I did it into the themes functions.php file):
//This allows us to grab the ID of an attachement by it's URL) function pippin_get_image_id($image_url) { global $wpdb; $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); return $attachment[0]; } add_shortcode( 'my_file_name', 'my_file_name_func'); // Actually activate the shortcode function my_file_name_func($atts) { global $post; // So we can get the post meta later on //define ShortCode attr prefix $types = "wpcf-{$atts['types_field']}"; if ($types) { // if the types_field argument was provided $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values $content = ''; // Setting up a variable to hold the links so we can return it later foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values $arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename $id = pippin_get_image_id($fileurl); $title = get_the_title($id); $content .= '<a href="'.$fileurl.'" title="'.$title.'" target="_blank">'.$title.'</a><br/>'; // Create whatever HTML and store it in the $content variable } return $content; // Return the content as the shortcode value } }
Then use the following shortcode, replacing the field appropriately:
[my_file_name types_field="toolset-file-field"]
The only problem with the above is that if you don't have a document uploaded in a particular custom field, you get the Title of the post returned with a link back to the same page you're on. I'm trying to figure out how to make this not be the case, and I will update here if I discover it, unless someone else can shed some light in the mean time.
Thanks
Andy
Ok, sussed the code..
Add the following into your Functions.php file etc etc...
//This allows us to grab the ID of an attachement by it's URL) function pippin_get_image_id($image_url) { global $wpdb; $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); return $attachment[0]; } add_shortcode( 'my_file_name', 'my_file_name_func'); // Actually activate the shortcode function my_file_name_func($atts) { global $post; // So we can get the post meta later on //define ShortCode attr prefix $types = "wpcf-{$atts['types_field']}"; if ($types) { // if the types_field argument was provided $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values $content = ''; // Setting up a variable to hold the links so we can return it later $testurl = ''; //Creating URL Variable to Test Later foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values $arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename $id = pippin_get_image_id($fileurl); $title = get_the_title($id); $content .= '<a href="'.$fileurl.'" title="'.$title.'" target="_blank">'.$title.'</a><br/>'; // Create whatever HTML and store it in the $content variable $test_url = $fileurl; } } if (!empty($test_url)) { return $content; // Return the content as the shortcode value } else { return 'Sorry, we do not have this document available.'; //Polite message if no file found. } }
I've added in a $test_url variable into the foreach loop. This test_url variable will only get a value if there is a document in existence due to the way the rest of the short code has been written.
I've then pulled the return content element out into it's own if clause so that if the test_url variable isn't empty, it will display the list of files with proper links and pull in the title from the media library. If, however, the test_url is empty, it pops out a polite message )which you can edit if you wish)
Use the shortcode as below changing the field value as appropriate.
[my_file_name types_field="toolset-file-field"]
All good, but would be good to see something like this as a standard option.
Hi Andy,
Thank you for sharing your completed solution and I'm sure it will help other users with a similar requirement.
The reason my proposed solution may not have worked on your website could be the difference in how the Divi's theme builder handles the shortcodes within shortcodes. But constructing the complete link output in custom PHP first and then returning it (as you've done in your shortcode) is the right approach, in this case.
I can see how a built-in feature for this can be useful in a number of cases and will pass on the feedback internally.
regards,
Waqar