Support » Plugin: Lana Downloads Manager » Downloads in Advanced Custom Fields as Post Type

  • I recently got downloads to work in ACF (Advanced Custom Fields) Repeater Fields as Post Objects and thought someone else might be able to use or build on what I figured out.

    By choosing Post Object as the field type my client can create a list of downloads on a page by simply selecting from a list of available downloads as created in Lana Download Manager. In the code I can pull the name, the URL, etc.

    Note that when creating the Post Object field in ACF you’ll want the Return Format to be set as Post Format not Post ID.

    if( have_rows('product_downloads') ): // Parent Repeater ?>
    	<div class="product-downloads">
    			while( have_rows('product_downloads') ): the_row();
    				$product_download = get_sub_field('product_download'); // Repeating field
    				//print_r( $product_download ); // Un-comment print_r to view result in browser - you can include attributes from the list (see post_name and post_title below)
    				// override $post
    				$post = $product_download; // original var must be $post
    				setup_postdata( $post ); // make this our active post
    				$post_ref = get_post();
    				$download_slug = $post_ref->post_name;
    				$download_name = $post_ref->post_title; ?>
    				<li class="product-download">
    					<a href="/download/<?php echo $download_slug ?>"><?php echo $download_name; ?></a></li>
    				wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly  ?>
    			endwhile; // have_rows ('product_downloads') ?>
    endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Lana Codes


    Awesome. Thank you.

    Thread Starter thefieldset


    In this code, what else is available via get_post() as expressed in the variable $post_ref->?

    Among other things, can I also access the actual file URL (not just the download URL)?

    Thanks in advance.

    • This reply was modified 1 year, 2 months ago by thefieldset.
    Plugin Author Lana Codes


    default as post:

    get_the_post_thumbnail( $post_ref->post_id, 'thumbnail' );

    $file_url = get_post_meta( $post_ref->post_id, 'lana_download_file_url', true );
    $download_count = get_post_meta( $post_ref->post_id, 'lana_download_count', true );

    custom functions:
    lana_downloads_manager_get_download_url( $post_ref->post_id );
    lana_downloads_manager_get_download_shortcode( $post_ref->post_id );

    However, you cannot use the file url in most cases. Direct access is disabled. You can only access it through the server.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Downloads in Advanced Custom Fields as Post Type’ is closed to new replies.