Skip Navigation

[Resolved] Display File Download link as PHP, Repeater field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

This topic contains 15 replies, has 5 voices.

Last updated by RobbieS 9 years, 8 months ago.

Assisted by: Ana.

Author
Posts
#235944

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>
#235953

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/

#236135

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?

#236213

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.

#238522

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?

#239878

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

#240897

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.

#241921

Ana
Supporter

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

#242130

Thanks Ana

#242139

Ana
Supporter

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

#242710

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?

#242821

Ana
Supporter

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

#243642

Perfect! Fantastic support as always, thanks Ana!

#243963

Ana
Supporter

Hi Ben,

if all is resolved for you can we close this ticket?

Thanks,
Ana

#243972

Thanks!

The forum ‘Types Community Support’ is closed to new topics and replies.

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