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?
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.
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!
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.