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.
<?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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Downloads in Advanced Custom Fields as Post Type’ is closed to new replies.