Hi,
I have a Custom field to upload multiple Files for visitors to download on the front end.
How do I the PHP code in my template to display the list of File Downloads attached to the post? They should display the file name, and open the file in a new page.
Example:
<ul>
<li><a ref="#" target="_blank">Download This</a></li>
<li><a ref="#" target="_blank">Download That</a></li>
<li><a ref="#" target="_blank">Download The Other</a></li>
</ul>
Dear benG-3,
Please take a look at this page and let me know if it solves your issue: https://toolset.com/documentation/user-guides/displaying-wordpress-custom-fields/
Hi Adriano,
Thanks for your reply. I did look at that page but I'm still confused.
I'm not sure how to output a list of downloads with the file name showing which links to the file?
Dear benG-3,
You don't need to use PHP to display that, you can create a Content Template and then display the repeating fields as it is explained here: https://toolset.com/documentation/user-guides/repeating-fields/#Displaying Repeating Fields
Please let me know if you are satisfied with my answer and if I can help you with any other related question.
Hi Adriano,
Thanks, I can now display the Repeater Download items in an unordered list. But how can I add an <a> link to each list item, so that when the User clicks on it it opens up that link?
Dear Ben,
I think that what you are looking for is something like this:
<ul>
[wpv-for-each field="wpcf-images"]
<li><a ref="[types field="images" raw="true"]" target="_blank">Download This</a></li>
[/wpv-for-each]
</ul>
Let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Caridad
Hi Caridad,
Thanks for your reply.
How do I insert this into a Template? It doesn't seem to be working: hidden link
The repeater fields should appear as a list in the downloads box.
Dear Ben,
I need to request temporary access (wp-admin and FTP) to your site – preferably to a test site where the problem has been replicated if possible – in order to be of better help. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.
Thanks,
Ana
Dear Ben,
in case there is opening and closing shortcodes the correct way to use in php templates would be:
echo do_shortcode('[wpv-for-each field="wpcf-images"]'.$text_to_be_wrapped_in_shortcode.'[/wpv-for-each]');
Link to WordPress codex page with corresponding documentation: http://codex.wordpress.org/Function_Reference/do_shortcode
however I think that an easier way to accomplish what you wanted was to create a content template 'publication single downloads' with the following:
<ul>
[wpv-for-each field="wpcf-downloads"]
<li><a href="[types field="downloads" raw="true"][/types]">Download File</a></li>
[/wpv-for-each]
</ul>
then add the render of this content template in your php template file:
<?php echo render_view_template('185'); ?>
note that the '185' is the ID of the content template previously created.
More information on how to render Views shortcodes in a php template in the documentation: https://toolset.com/documentation/user-guides/views-api/
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Ana
Hi Ana,
Perfect, thanks so much for your help!
One last question, is there any way I could name these Downloads dynamically? Instead of Download File, Download File. It would be perfect to display the name of the file somehow?
Hi Ben,
for that I can suggest you use a custom shortcode that would return the name of the file that is being displayed. You can use the following in your content template:
<ul>
[wpv-for-each field="wpcf-downloads"]
<li><a href="[types field="downloads" raw="true"][/types]">[get-download-name field="wpcf-downloads" post_id="[wpv-post-id]"]</a></li>
[/wpv-for-each]
</ul>
and add the following custom shortcode function to your theme functions.php file:
add_shortcode( 'get-download-name', 'prefix_get_download_name' );
function prefix_get_download_name( $atts ) {
$a = shortcode_atts( array(
'field' => '',
'post_id' => ''
), $atts );
$out = __('Download file', 'toolset');
$index = apply_filters('wpv-for-each-index', 0);
$meta_values = get_post_meta( $a['post_id'], $a['field'], false );
if (!empty($meta_values[$index])){
$out = basename($meta_values[$index]);
}
return $out;
}
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Ana
Perfect! Fantastic support as always, thanks Ana!
Hi Ben,
if all is resolved for you can we close this ticket?
Thanks,
Ana