Skip Navigation

[Resolved] Display attachment title in button for multiple attached files

This support ticket is created 4 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 3 replies, has 2 voices.

Last updated by davidI-6 4 years ago.

Assisted by: Waqar.

Author
Posts
#1579989

Tell us what you are trying to do?

Show file attachment title in button

Is there any documentation that you are following?

https://toolset.com/forums/topic/display-multiple-attached-files-as-download-links/#post-506926
https://toolset.com/documentation/user-guides/views/shortcodes-within-shortcodes/

Is there a similar example that we can see?
hidden link
(see three Datasheet buttons without file name)

What is the link to your site?

hidden link

Following the above post, I've added this code to my functions.php to register the shortcode

// Add file title to button

add_shortcode( 'wpv_display_file_name', 'wpv_display_file_title_func');
function wpv_display_file_title_func($atts) {
 
    global $wpdb;
    $atts = shortcode_atts( array(
        'url' => '',
        'info' => '', // title, alt or id return
    ), $atts);
    $res = '';
    $url = $atts['url'];
    $attachment_id = $wpdb->get_var($wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE guid = %s", $url
    ));
    if($atts['info'] == 'title'){
        $res = get_the_title($attachment_id);
    }
    if($atts['info'] == 'alt'){
        $res = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    }
    if($atts['info'] == 'id'){
        $res = $attachment_id;
    }
    return $res;
}

I've registered custom shortcode [wpv_display_file_name] in Toolset > Settings > Front-End Content > Third-party shortcode arguments. Note. the custom shortcode doesn't show in toolset shortcodes > fields and views list.

Then tried this code on my custom template to display the buttons

[wpv-for-each field="wpcf-product-specifications-2"]
<a href="[types field='product-specifications-2' link='true' output='raw'][/types]" target="_blank" >Download - [wpv_display_file_name info="title" url="[types field='product-specifications-2' link='true' separator=', ' output='raw'][/types]"]</a>
[/wpv-for-each]

Also tried to display one a the time without loop

{!{types field='product-specifications-2' title='{!{wpv_display_file_name file_url="{!{types field='product-specifications-2' link='true' output='raw'}!}{!{/types}!}"}!}' index='1' separator=', '}!}{!{/types}!}

Thanks in advance for your help

David

#1580941

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi David,

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

To troubleshoot this on a test website and suggest the best way forward, I'll need to see exactly how this field and the content template is set up.

Can you please share temporary admin login details in reply to this message?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1584709

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi David,

Thank you for waiting while I performed some testing and research.

I found that that the nested shortcode is not directly supported inside the "WPBakery" builder and requires some extra customization steps:
hidden link

A simple workaround can be creating a separate Toolset content template set with the classic editor and then placing the nested shortcode in that:


[wpv-for-each field="wpcf-product-specifications-2"]
<a href="[types field='product-specifications-2' link='true' output='raw'][/types]" target="_blank" >Download - [wpv_display_file_name info="title" url="[types field='product-specifications-2' link='true' output='raw'][/types]"]</a><br>
[/wpv-for-each]

Next, you can add the shortcode for that new content template inside the actual "Products-VC" template:

[php]

[wpv-post-body view_template="ct-for-nested-shortcode"]

</code

Note: please replace "ct-for-nested-shortcode" with the actual slug of your newly created content template.

I hope this helps and please let me know how it goes.

regards,
Waqar

#1585069

Thanks Waqar for the detailed solution! It works as intended now

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