Skip Navigation

[Resolved] How to display just the file name for a repeater field block from a file field

This support ticket is created 3 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.

This topic contains 2 replies, has 2 voices.

Last updated by Stéphane 3 years, 3 months ago.

Author
Posts
#2136677

I'm using a repeater field in a content template to display a list of links to PDF files entered from a Types file field (repeating). The block displays the entire URL but I just want the file name.

I would have really though this would be an option directly in the block's editing interface but I do not mind doing custom code. Except I'm not finding much useful information and I really don't know where to start so any guidance would be much appreciated.

FWIW, I'm coming back to Toolset after not using it for a while so I'm almost starting from scratch 😉

Thank you!

#2137329

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You are right that this should really be an option when outputting the file field, but it's not, so you will need to add some custom code to help get the result you need.

First, note that file uploads are handled via the WP Media Library, and as such for each file field value there is a corresponding media post entry, the title of which is automatically the name of the file (rather than its full path), which is what you need.

Previously I have shared a somewhat generic custom shortcode for outputting the post field values from these corresponding posts generally (e.g. the alt attribute of a media post starting with an image URL), and you can use the same custom shortcode for this purpose.

See hidden link

Add that code to your site (you can add it as a code snippet at Toolset > Settings > Custom Code) to register the media-field shortcode.

Because we are working with custom shortcodes, you can't simply use the output of blocks here, you'll need to work with shortcodes for outputting Toolset field values.

If this were a non-repeating field, you'd do something simple such as:

[media-file][types field="my-pdf-field"][/types][/media-file]

and that would output the file name.

But with a repeating field this needs an extra step, whereby we use the wpv-for-each shortcode (https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-for-each) to iterate over the field values.

To display your filenames as an unordered list, for example, you would need the following:

<ul>
[wpv-for-each field="wpcf-my-pdf-field"]
<li>[media-field][wpv-post-field name="wpcf-my-pdf-field"][/media-field]</li>
[/wpv-for-each]
</ul>
#2141607

Hi,

Sorry for the delay in responding, I've been sidetracked by other projects this week. I managed to create a working solution from other threads I found here.

First, in the repeating field block I have this code :

<ul class="pdf-list">
[wpv-for-each field="##FIELD_SLUG##"]
<li><a href="[wpv-post-field name="wpcf-file"]">[get-file-title url='[wpv-post-field name="wpcf-file"]'].pdf</a></li>
[/wpv-for-each]
</ul>

The "get-file-title" shortcode looks like this with a small function I found that returns an attachment's ID:

function zw3_get_image_id($attachment_url) {
	global $wpdb;
	$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $attachment_url ));
	return $attachment[0];
}

add_shortcode( 'get-file-title', 'zw3_get_file_title_func');
function zw3_get_file_title_func( $atts ) {
	$url = $atts['url'];

	$attachment_id = zw3_get_image_id( $url );

	if( $attachment_id > 0 ) {
		$attachment_title = get_the_title( $attachment_id );
	}

	if ($attachment_title) {
		return $attachment_title;
	}
}

Thank you!