Skip Navigation

[Résolu] Display file name of uploaded file

This support ticket is created Il y a 8 années et 5 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Marqué : 

This topic contains 6 réponses, has 4 voix.

Last updated by mandyS Il y a 8 années et 4 mois.

Assisted by: Minesh.

Auteur
Publications
#349880
display-title.png

Hi, I am trying to: display the 'title' of a file in Views

I visited this URL: https://toolset.com/forums/topic/upload-file-title-custom-fields/

but I couldn't work out what is going on there. Ideally would like to add some shortcode within shortcode so I can get the files title to display. Like this:

[types field="graph-file" link="true" title="[file_title]" separator=", "]

It would be great if this could be added as a feature of Toolset - so that there is a conditional option within the file field to display the title - rather than everyone having to write php code or create complex workarounds 😉

#349949

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Actually, the media uploaded in the media library, is treated as the 'attachment' post type. The standard way to get title from media image.
=> https://codex.wordpress.org/Function_Reference/wp_get_attachment_image#Display_all_images_as_a_list

You may refer to following shortcode to retrieve image title/caption, using the following code:

function my_images_shortcode( $atts) {
extract( shortcode_atts( array(
    'img' => '',
), $atts ) );
  
global $post, $wpdb;
    
$images = get_post_meta($post->ID, $img, false);
$out = '';
foreach ($images as $image) {
$attachment_id = $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid = %s",
$image
));
$image_title = get_the_title($attachment_id);

$caption = get_the_excerpt($attachment);
$description = get_the_content($attachment_id); 

$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
$out .= '<img itemprop="image" src="' . $image . '" alt="' . $alt . '" title="' . $image_title . '" class="attachment-thumbnail" />';
}
$out .= '';
return $out;
}
add_shortcode('my-images', 'my_images_shortcode');

You can call this like:

[my-images img='wpcf-carousel-item-1']

Where:
please replace "wpcf-carousel-item-1" with your original types field.

#350090

Wow thanks Minesh, that doesn't look very easy to me! It's actually pdf files that I'm trying to get the title of.

Can this code be adapted to output the title of a pdf?

#350111

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes, it should work for PDF file as well.

See this reference ticket.
=> https://toolset.com/forums/topic/display-media-library-title-and-description-of-a-pdf-file-field-in-php-code/

#350690

Hi Minesh, sorry for my slow response. In the end I had to pay a php coder to do this for me as I couldn't work it out.

This is the working code:

//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-graph-file',
), $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;
}

Seems like a lot of hassle just to be able to display a file name!

Currently you can only display a file url on the frontend unless you can code in php or have time to create a whole new custom post type just to display a file name.

Hopefully this will be be a standard feature in the future. 😉

#561576

Hi, morktron: I have this same request you did to display the 'title' of a file in Views. I see that you had a custom PHP shortcode to accomplish this. I tried your [upload-file-title] shortcode but it returned an empty Array on the front-end. I wanted to ask if there were additional attributes I'm supposed to use within the shortcode? Such as [upload-file-title types_field="graph-file"]? Thanks in advance!

#575699

Hello JeremyM3920,
Were you able to work it out? Could you kindly share the code to me? 🙂

Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.