php - Related Products ACF WordPress -
i not using woocommerce plugin, normal site.
i have page, need let user select 'related products'.
now, using acf, , looking @ using post_object allow user select product.
what needs do, product name, products image , description.
i have used code, acf site, attempt grab post object title.
<?php $post_object = get_field('post_object'); if( $post_object ): // override $post $post = $post_object; setup_postdata( $post ); ?> <div> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <span>post object custom field: <?php the_field('field_name'); ?></span> </div> <?php wp_reset_postdata(); // important - reset $post object rest of page works correctly ?> <?php endif; ?>
but won't display anything?
is there obvious issues can see?
since allowing multiple selections, get_field return array of post objects. need loop through array, using following code. code assumes post object field named 'related_products', , calls post's excerpt value, check excerpt field first, , if not present, generate excerpt content of post.
$related_products = get_field('related_products'); if( $related_products ): ?> <ul> <?php foreach( $related_products $post): // variable must called $post (important) ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php the_excerpt(); ?> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); // important - reset $post object rest of page works correctly ?> <?php endif;
Comments
Post a Comment