Skip Navigation

[Resolved] File upload – Display title

This thread is resolved. Here is a description of the problem and solution.

Problem:

Display file title and link of custom repeating field.

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/file-upload-display-title/#post-1305207

Relevant Documentation:

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

Last updated by Vindfang 5 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1304839

Users are going to upload attachment for announcements. The attachments needs to be displayed with title, and not just the URL.

I have followed this thread: https://toolset.com/forums/topic/displaying-file-name-title-uploaded-from-admin/
But this did not solve my problem, as it only shows "[/types]» info=»title»]" on frontend.

PHP:

add_shortcode( 'show_file_info', 'func_show_file_info');
function func_show_file_info($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;
}

Backend:

[show_file_info url="[types field='vedlegg' output='raw'][/types]" info="title"]

Frontend:
[/types]» info=»title»]

How can this be solved?

Looking forward to hear from you,

#1304883

Hi,

Since it is a custom PHP codes problem, please check these:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/

2) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2019, deactivate all custom PHP/JS code snippets, and test again

If the problem still persists, please provide a test site with the same problem, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug, thanks

#1304993

Thanks for the details, I can log into your website, here are what I found:
1) You are using repeating image field, so that custom shortcode won't be able to work as expected.
2) And the the text widget won't work for the custom shortcode.

I have done below modifications in your website:
1) Edit your theme file "functions.php", replace this line from:

...
    $url = $atts['url'];
...

To:

...
    $url = do_shortcode($atts['url']);
...

So it will be able to parse the Types shortcode.

2) Edit the content template
hidden link

Add a WP Text widget, with below codes:

[wpv-for-each field="wpcf-vedlegg"]
[show_file_info url="[types field='vedlegg' output='raw'][/types]" info="title"]
[/wpv-for-each]

Please test again, check if it is what you want.

More help:
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.

#1305063

Great, thanks alot Luo Yang! I believe we are almost there.

It would be great if the title was with a link, so the URL is not visible.

Like this:

<a href="URL" target="_blank">Title</a>

Can you do those changes for me?

#1305101

I have modified the codes in "WP Text widget" as below:

[wpv-for-each field="wpcf-vedlegg"]
<a href="[types field='vedlegg' output='raw'][/types]" target="_blank" rel="noopener noreferrer">[show_file_info url="[types field='vedlegg' output='raw'][/types]" info="title"]</a>
[/wpv-for-each]

Please test again, thanks

#1305163

Hold on, above codes won't work in your website. will update here soon

#1305165

Yes, I saw the link came twice. Just took over the page and was going to take a look at it, then you commented. But I left the content block now 🙂

#1305207

I have modified the PHP codes as below:

add_shortcode( 'show_file_info', 'func_show_file_info');
function func_show_file_info($atts) {
   
    global $wpdb;
    $atts = shortcode_atts( array(
        'url' => '',
        'info' => '', // title, alt or id return
    ), $atts);
    $res = '';
    $url = do_shortcode($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;
    }
    if($atts['info'] == 'url-title'){
        $res = '<a href="' . $url . '" target="_blank">' . get_the_title($attachment_id) . '</a>';
    }
    return $res;
}

Add an option "url-title" in custom shortcode, for example:
[show_file_info ... info="url-title"]
It can output the file title + URL.

2) Edit the codes of in "WP Text widget", as below:

[wpv-for-each field="wpcf-vedlegg"]
[show_file_info url="[types field='vedlegg' output='raw'][/types]" info="url-title"]
[/wpv-for-each]

Please test again, check if it is fixed, thanks

#1305255

Great! Thanks a lot Luo Yang! My issue is resolved now.