Skip Navigation

[Resolved] Truncate file full path url to only filename

This support ticket is created 5 years 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
- 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)

This topic contains 6 replies, has 2 voices.

Last updated by Yannis Sintos 5 years ago.

Assisted by: Waqar.

Author
Posts
#1389079

Hello,

In custom fields of posts, i enter a reccuring field with "file attachments" to each post.
A sample post you can see is here hidden link

My question is, instead of showing the full path of the file, is it possible to just present the filename or the file description ?

Thank you

#1389235

Hi Yannis,

Thank you for contacting us and I'd be happy to assist.

To extract the file name, from the file type custom field, you'll need a custom shortcode:


add_shortcode( 'get_file_name_custom', 'get_file_name_custom_func');
function get_file_name_custom_func($atts) {
	$a = shortcode_atts( array(
		'url' => ''
	), $atts );

	$url_arr = explode('/',$a['url']);
	
	return end($url_arr);
}

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 show only the file names from the repeating file type custom field, like this:


[wpv-for-each field="wpcf-file-field-slug"]
[get_file_name_custom url="[types field='file-field-slug' output='raw'][/types]"]<br>
[/wpv-for-each]

Note: please replace "file-field-slug" with the actual slug of your file type field.

Related documentation links:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each
https://toolset.com/documentation/customizing-sites-using-php/functions/#file

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1389309

I implemented the function in the function.php and i tried to enter the code in the content template, but the files are not links.
Can i make them linkable to the actual files ?

#1389319

Yes, to convert the file names to the links to the actual files, you can use the shortcodes like this:
( ref: hidden link )


[wpv-for-each field="wpcf-file-field-slug"]
<a href="[types field='file-field-slug' output='raw'][/types]" target="_blank">[get_file_name_custom url="[types field='file-field-slug' output='raw'][/types]"]</a><br>
[/wpv-for-each]

#1389349

The issue is solved in a great way !!!
Thank you so much.

#1389389

Hello again.

Huge error...
In the files list you can see in hidden link
The links are not FROM the attached files (from each file).
They are the link of the post !!!!

#1389415

I made a mistake in typing...

My issue is resolved now. Thank you!