For a custom post type, I have the File post field (screenshot: hidden link ) which I'm then displaying on the front-end using Toolset Content Templates/Views. Currently, link has the file path being displayed (screenshot: hidden link).
Rather than the file path being displayed, I would like a 'human-readable' name such as the Title of the file as what's in the WP Media Library (screenshot: hidden link). How can I create a shortcode that I can use in Content Templates/Views that will take the File post field and display the link with a human-readable name using the WP Media Library Title field?
Thank you in advance for your help!
It's natively not possible.
You need Custom Code for this, or, add manually a title to the GUI when you insert the File ShortCode.
Some Custom Code example can be seen here:
https://toolset.com/forums/topic/print-out-file-name-in-view-template/#post-34741
Hi, Beda. Thanks for the link for that custom code. I'm actually already using that code to display the file path. Can you please guide me in how to modify that code so that it displays the Title of the file as what's in the WP Media Library (screenshot: hidden link)? Would greatly appreciate further guidance. Thanks again, Beda!
Update: I found this post (https://toolset.com/forums/topic/display-media-library-title-and-description-of-a-pdf-file-field-in-php-code/#post-261596) which seems to accomplish what I'm trying to. However, when I use the [upload-file-title] shortcode in the Content Template (screenshot: hidden link), the frontend displays 'Array' so I know the code is not working (screenshot: hidden link). I'm pasting the code below in case you're able to troubleshoot this, please? By the way, the field slug is 'report' (screenshot: hidden link).
//File title
add_shortcode('upload-file-title', 'upload_file_title_func');
function upload_file_title_func($atts){
global $wpdb;
extract( shortcode_atts( array(
'field' => 'wpcf-report',
), $atts ) );
$file_array = get_post_meta(get_the_ID(), $field);
if(!empty($file_array))
{
$res = '';
foreach($file_array as $k => $v)
{
$id = url_to_postid($v);
if($id==0) {
$sql = "SELECT ID FROM $wpdb->posts WHERE guid= '$v'";
$id = $wpdb->get_var($sql);
}
$title = get_the_title($id);
$value = explode(',', $title);
$res .= '' . wp_get_attachment_link( $id, '', false, false, $value) . ', ';
}
$res .= '';
}
return $res;
}
It depends what field you have. If it's repeatable or not.
You need to adjust the Code within your ShortCode to get the Title of that attachment:
$attachment_title = get_the_title($attach_id)
http://codex.wordpress.org/Function_Reference/get_the_title
So that means, you need the ID first, and that needs Custom Code. Some guidance:
https://stackoverflow.com/questions/25671108/get-attachment-id-by-file-path-in-wordpress
Unfortunately, this is not something supported by Toolset at the moment.
It seems -unfortunately- that for now, you need custom programming work which is beyond the scope of our support.
At this point I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/
You will get the custom assistance you need to get on with your project.
Hi, Beda:
The post type field in question here is a File repeater field.
The $attachment_title = get_the_title($attach_id) code gets the title from the featured image attachment, not from the Toolset File repeater field.
Do these answers help you with knowing if there's a different piece of code I should be using?
You need to foreach over the single repeating instances, and then use the WordPress API to get_the_title() from the Attachements ID.
I found some information online and created this working sample, which allows you to output all Repeating Fields Titles, with a Custom ShortCode.
It can be edited so to make the output more customizable or nicer.
I assume a Types File Field (repeating) with slug "toolset-file-field".
I have 3 such files added to a Post and want to display those 3 File's Titles, on my Post body.
So I added this to the functions.php of your Theme:
(credits: hidden link)
//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];
}
Then I created a Custom ShortCode that uses the above function, so I added this to the functions.php just below it:
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 .= $title . '</br>'; // Create whatever HTML and store it in the $content variable
}
return $content; // Return the content as the shortcode value
}
}
Then I used this ShortCode in my Post Body:
[my_file_name types_field="toolset-file-field"]
This works flawlessly.
Can you confirm?
Yes, that works! A big thank you!
BTW, would love to see a future update of Toolset that has settings for the File upload post field to enable users to add a display title so it's not always a file URL that is outputted.
Is this feature available in the current Toolset version?