Skip Navigation

[Resolved] Custom function to display FILE field not working as expected …

This support ticket is created 3 years, 7 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
- 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/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2063451

Hi.

I have a FILE filed named "product-files" that can have multiple instances.

I have a custom function that will format the file names into clickable links, but it is not returning any values.

FUnction:

function my_term_files_func($atts){
  $atts = shortcode_atts( array(
    'field' => '',
  ), $atts );
   
  $field = $atts['field'];
  if ( ! $field ) return;
   
  $values = get_term_meta( get_queried_object_id(), $field, false );
  
  $out = "";
  foreach( $values as $value ){
    $out .= '<a href="' . $value . '" target="_blank">' . end( explode( '/', $value ) ) . '</a><br />';
  }
   
  return $out;
}
add_shortcode( 'term_files', 'my_term_files_func' );

I also registered the custom shortcode "term-files" in Toolset > Settings.

In the content-template ... I entered:

[term_files field="product-files"]

but this returns nothing.

have I done something wrong?

#2063825

Hello,

I assume you are using Toolset Types plugin to setup the custom taxonomy field "product-files", Toolset Types plugin will add prefix "wpcf-" before field slug, so you can try to modify your shortcodes as below:
[term_files field="wpcf-product-files"]
And test again.

More help:
https://toolset.com/documentation/customizing-sites-using-php/functions/

when you are accessing custom fields through native WordPress functions, you need to prepend the wpcf- prefix to the slug.

#2064801

ok.

so I got it working.

right now the function looks like this:

//	START my_post_files_func
function my_post_files_func($atts){
  $atts = shortcode_atts( array(
    'field' => '',
  ), $atts );
   
  $field = $atts['field'];
  if ( ! $field ) return;
   
  $values = get_post_meta( get_queried_object_id(), $field, false );
  
  $out = "";
  foreach( $values as $value ){
    $out .= '<a href="' . $value . '" download>' . end( explode( '/', $value ) ) . '</a><br />';
  }
   
  return $out;
}
add_shortcode( 'post_files', 'my_post_files_func' );
//	STOP my_post_files_func

i am calling it like this:

[post_files field="wpcf-ib-product-documentation"]

and it is returning the data as I requested:

<a href="' . $value . '" download>' . end( explode( '/', $value ) ) . '</a><br />

This is exactly what I was trying to do.

However, is there a way to access the WordPress "TITLE" attribute for the file? I would like to display that as the link text instead of the end part of the FILE URL like I am currently doing.

thx!

#2066017

For the new question:
is there a way to access the WordPress "TITLE" attribute for the file?

Please try the built-in Types shortcode [types]
https://toolset.com/documentation/customizing-sites-using-php/functions/#image

For the alt and title attributes, you can also use placeholders to output the values of standard image fields added in WordPress: %%TITLE%%, %%ALT%%, %%CAPTION %%, and %%DESCRIPTION%%.

If it is a multiple instances image field, please check this document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each
Iterate through multiple items in a post meta field and output the enclosed text for each item. Including Types repeating fields.