Community Forums › Forums › Archived Forums › General Discussion › Advanced Custom fields Post Object problem
Tagged: ACF, advanced custom fields
- This topic has 4 replies, 2 voices, and was last updated 7 years, 11 months ago by agelfand.
-
AuthorPosts
-
November 10, 2016 at 10:22 am #195991agelfandMember
Hi, I am trying to incorporate ACF into the Metro theme. (I am rebuilding an existing site using Genesis; the code I am now asking about worked in that theme).
I can get the simple custom field data to display, but I am having a problem getting the function to loop through multiple Post Objects. Here is the tnire function; it outputs custom field data after the main content on a custom post. The Post Objects are for a You May Also Like section. The author selects any number of posts in the ACF multi-select filed and then I output them in a nice list.
When I used ACF's debug snippet, I wasable to verify that the Post Object data does exist. But when I try to pull in my Post Object query, all I get is the current post's data repeated over and over again, not the posts that were selected in the ACF field.
My guess is that the problem is creating a seconday query loop in Genesis, but I just don't know what specifically is wrong with what I am doing. Have not found documentation anywhere on the interwebs about Post Objects and Genesis. THANKS in advance!
add_action ('genesis_entry_content', 'etb_acf_posts' ); function etb_acf_posts() { if (!is_singular(array('speakers','live_arts','event_production','films','presentations')) ) { return; } // else { $bookinglink = get_field('booking_url'); echo '<a href="'.$bookinglink.'">'; $btntext = get_field('booking_button_text'); if ($btntext == '') { echo 'Book '; echo the_title(); } else { echo $btntext; } echo '</a>'; $video1 = get_field( 'video_one'); if( isset($video1) && $video1 != '' ){ echo '<h4>Video Gallery</h4>'; echo '<div class="one-third first">'; echo $video1; echo '</div>'; } $video2 = get_field( 'video_two'); if( isset($video2) && $video2 != '' ){ echo '<div class="one-third">'; $video2 = get_field( 'video_two'); echo $video2; echo '</div>'; } $video3 = get_field( 'video_three'); if( isset($video3) && $video3 != '' ){ echo '<div class="one-third">'; $video3 = get_field( 'video_three'); echo $video3; echo '</div>'; } echo '<div class="clear"></div>'; $gallery_id = get_field('nextgen_gallery_id'); if( isset($gallery_id) && $gallery_id != '' ){ echo '<h4>Image Gallery</h4>'; echo "<div class=\"post-gallery\">"; // echo nggShowGallery( $gallery_id ); // Display the gallery echo do_shortcode( $gallery_id ); echo "</div><div class='clear'></div>"; } echo '<div class="post"><h4>Topics</h4>'; function mytheme_filter_tags( $term_links ) { $result = array(); $exclude_tags = array( 'featured' ); foreach ( $term_links as $link ) { foreach ( $exclude_tags as $tag ) { if ( stripos( $link, $tag ) !== false ) continue 2; } $result[] = $link; } return $result; } add_filter( "term_links-post_tag", 'mytheme_filter_tags', 100, 1 ); echo get_the_tag_list('<div class="entry"><h4> ',', ','</h4></div>'); echo '</div>'; remove_filter( "term_links-post_tag", 'mytheme_filter_tags', 100 ); } //HERE IS THE PART I AM HAVING TROUBLE WITH $posts = get_field('you_might_also_like'); if( $posts ): foreach( $posts as $post): // variable must be called $post (IMPORTANT) setup_postdata($post); ?> <div class="block"> <div class="fix"></div> <h2 class="cufon"><?php the_title(); ?></h2> <?php the_excerpt(); ?> <span class="more"><a>"><?php _e('More Info', 'woothemes') ?></a></span> </div> <?php endforeach; ?> <div class="fix"></div> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly endif; }
The site is password protected. User is demo, password is 12cce8
http://etb.wpengine.com/speakers/alex-grey-2/November 12, 2016 at 9:57 pm #196081Brad DaltonParticipantHard to test because it relies on a 3rd party plugin setup as well as setup of multiple CPT's.
I suggest you paste the code into a Github Gist and link to it.
November 14, 2016 at 9:43 am #196136agelfandMemberWell, can anyone speak to the general issue of having secondary loop queries in genesis? It's frustrating that this widely used plugin doesn't have much in the way of documentation for use with Genesis. I would have guess the two are widely used together. Of course, I've been wrong before! 🙂
https://gist.github.com/anonymous/232ad6bebf6b3a1b3e6c2e5093e1be95
November 15, 2016 at 1:57 pm #196193agelfandMemberHi, it's me again. 🙂
I placed my ACF fields in a template, to see if things would work inside a custom loop I crreated. Everything works except the POST OBJECT at the end. Can someone at least look at my code and see if there is an obvious fumble on my part? As with my custom function in functions.php, even though the $posts array with my custom data is in there, I can't get it to echo out. It just runs through however many post objects there are and echoes the title of the current post. This is a "you may also like" section.
remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop add_action( 'genesis_loop', 'custom_do_loop' ); // Add custom loop function custom_do_loop() { while(have_posts()) : the_post(); // Intro Text (from page content) echo '<div class="page hentry entry">'; echo '<h1 class="entry-title">'. the_title() .'</h1>'; echo '<div class="entry-content">' . the_content() .'</div>'; $bookinglink = get_field('booking_url'); echo '<a href="'.$bookinglink.'">'; $btntext = get_field('booking_button_text'); if ($btntext == '') { echo 'Book '; echo the_title(); } else { echo $btntext; } echo '</a>'; $video1 = get_field( 'video_one'); if( isset($video1) && $video1 != '' ){ echo '<h4>Video Gallery</h4>'; echo '<div class="one-third first">'; echo $video1; echo '</div>'; } $video2 = get_field( 'video_two'); if( isset($video2) && $video2 != '' ){ echo '<div class="one-third">'; $video2 = get_field( 'video_two'); echo $video2; echo '</div>'; } $video3 = get_field( 'video_three'); if( isset($video3) && $video3 != '' ){ echo '<div class="one-third">'; $video3 = get_field( 'video_three'); echo $video3; echo '</div>'; } echo '<div class="clear"></div>'; $gallery_id = get_field('nextgen_gallery_id'); if( isset($gallery_id) && $gallery_id != '' ){ echo '<h4>Image Gallery</h4>'; echo "<div class=\"post-gallery\">"; // echo nggShowGallery( $gallery_id ); // Display the gallery echo do_shortcode( $gallery_id ); echo "</div><div class='clear'></div>"; } echo '<div class="post"><h4>Topics</h4>'; //HERE IS THE PART I AM HAVING TROUBLE WITH // always good to see exactly what you are working with $posts = get_field('you_might_also_like'); // always good to see exactly what you are working with if( $posts ): foreach( $posts as $post): // variable must be called $post (IMPORTANT) setup_postdata($post); ?> <div class="block"> <div class="fix"></div> <h2 class="cufon"><?php the_title(); ?></h2> <?php the_excerpt(); ?> <span class="more"><a>"><?php _e('More Info', 'woothemes') ?></a></span> </div> <?php endforeach; ?> <div class="fix"></div> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly endif; endwhile; echo '</div><!-- end .entry-content -->'; echo '</div><!-- end .page .hentry .entry -->'; } /** Remove Post Info */ remove_action('genesis_before_post_content','genesis_post_info'); remove_action('genesis_after_post_content','genesis_post_meta'); genesis();
November 15, 2016 at 3:50 pm #196207agelfandMemberThe plot thickens! I moved the Post Object code outside the main loop. NOW I see the actual post objects selected in the back end through ACF, but they aren't being outputted in the page where I want them. Still, making progress. Can someone please help me along here? I need this post object to echo code before the end of the entry content...thank you.
<?php remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop add_action( 'genesis_loop', 'custom_do_loop' ); // Add custom loop function custom_do_loop() { while(have_posts()) : the_post(); // Intro Text (from page content) echo '<div class="page hentry entry">'; echo '<h1 class="entry-title">'. the_title() .'</h1>'; echo '<div class="entry-content">' . the_content() .'</div>'; $bookinglink = get_field('booking_url'); echo '<a class="button" href="'.$bookinglink.'" >'; $btntext = get_field('booking_button_text'); if ($btntext == '') { echo 'Book '; echo the_title(); } else { echo $btntext; } echo '</a>'; $video1 = get_field( 'video_one'); if( isset($video1) && $video1 != '' ){ echo '<h4>Video Gallery</h4>'; echo '<div class="one-third first">'; echo $video1; echo '</div>'; } $video2 = get_field( 'video_two'); if( isset($video2) && $video2 != '' ){ echo '<div class="one-third">'; $video2 = get_field( 'video_two'); echo $video2; echo '</div>'; } $video3 = get_field( 'video_three'); if( isset($video3) && $video3 != '' ){ echo '<div class="one-third">'; $video3 = get_field( 'video_three'); echo $video3; echo '</div>'; } echo '<div class="clear"></div>'; $gallery_id = get_field('nextgen_gallery_id'); if( isset($gallery_id) && $gallery_id != '' ){ echo '<h4>Image Gallery</h4>'; echo "<div class=\"post-gallery\">"; // echo nggShowGallery( $gallery_id ); // Display the gallery echo do_shortcode( $gallery_id ); echo "</div><div class='clear'></div>"; } echo '<div class="post"><h4>Topics</h4>'; endwhile; } //HERE IS THE PART I AM HAVING TROUBLE WITH // always good to see exactly what you are working with $posts = get_field('you_might_also_like'); // always good to see exactly what you are working with if( $posts ): foreach( $posts as $post): // variable must be called $post (IMPORTANT) setup_postdata($post); ?> <div class="block"> <div class="fix"></div> <h2 class="cufon"><?php the_title(); ?></h2> <?php the_excerpt(); ?> <span class="more"><a href="<?php the_permalink(); ?>"><?php _e('More Info', 'woothemes') ?></a></span> </div> <?php endforeach; ?> <div class="fix"></div> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly endif; echo '</div><!-- end .entry-content -->'; echo '</div><!-- end .page .hentry .entry -->'; /** Remove Post Info */ remove_action('genesis_before_post_content','genesis_post_info'); remove_action('genesis_after_post_content','genesis_post_meta'); genesis();
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.