• Resolved thefieldset

    (@thefieldset)


    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.

    <?php
    if( have_rows('product_downloads') ): // Parent Repeater ?>
    	<div class="product-downloads">
    		<ul>
    			<?php
    			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>
    				<?php
    				wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly  ?>
    			<?php
    			endwhile; // have_rows ('product_downloads') ?>
    		</ul>
    	</div>
    <?php
    endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Lana Codes

    (@lanacodes)

    Awesome. Thank you.

    Thread Starter thefieldset

    (@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 4 years, 2 months ago by thefieldset.
    Plugin Author Lana Codes

    (@lanacodes)

    default as post:
    $post_ref->post_id;
    $post_ref->post_title;
    $post_ref->post_excerpt;
    $post_ref->post_content;

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

    meta:
    $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.