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,
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
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.
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?
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
Hold on, above codes won't work in your website. will update here soon
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 🙂
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
Great! Thanks a lot Luo Yang! My issue is resolved now.