Skip Navigation

[Resolved] File post field: displaying file ‘Title’ text rather than file path

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I display the file path or title of an uploaded file in a file field of Toolset?

Solution:
It requires Custom Code.

https://toolset.com/forums/topic/file-post-field-displaying-file-title-text-rather-than-file-path/#post-562737

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 9 replies, has 3 voices.

Last updated by Beda 6 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#561437

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!

#561491

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

#561564

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!

#561575

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

#562265

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.

#562514

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?

#562737

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?

#562991

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.

#1133170

Is this feature available in the current Toolset version?

#1133224