简体   繁体   中英

displaying a relationship acf custom field on product category page

i'm using advanced custom field on my website. I've created a custom field "relationship" to be displayed on all my product category page (i'm using woocommerce, that's why I'm using product_cat_ instead of category in my php).

When using a basic text field, I'm abble to display the text on my category page, using this code :

<?php

$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;

?>

<div><?php the_field('text', $post_id); ?></div>


<?php ?>

but now when trying to use the relationship function inside this category page, I don't get the correct title and permalink from the post I've choose, and I can't find how to modify my code...

here is my code, my custom field is named mise_en_avant_produit , and it returns a post object.

<?php

$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$posts = get_field('mise_en_avant_produit', $post_id);
if( $posts ): 
?>

<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

<?php ?>

I've already used this kind of code, to display relationship fields from another page for example, but here I can't find the solution,

here is what I get when print_r

Array ( [0] => WP_Post Object ( [ID] => 42 [post_author] => 1 [post_date] => 2014-09-16 17:22:07 [post_date_gmt] => 2014-09-16 16:22:07 [post_content] => . [post_title] => Green tea [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => green-tea [to_ping] => [pinged] => [post_modified] => 2014-09-25 08:36:41 [post_modified_gmt] => 2014-09-25 07:36:41 [post_content_filtered] =>
[post_parent] => 0 [guid] => http://localhost:8888/bemygift/?product=green-tea [menu_order] => 0 [post_type] => product [post_mime_type] => [comment_count] => 0 [filter] => raw ) )

can anybody help me with this ?

thanks a lot,

I had similar problem, maybe this help you.

<?php while ( have_posts() ) : the_post(); ?>
                <?php 
                    $auth_name = get_field('first_last_name');
                    $auth_office = get_field('position_of_author');
                    $auth_photo = get_field('author_photo');
                ?>
                    <img class="alignleft" src="<?php echo $auth_photo['url']; ?>" alt="<?php echo $auth_photo['alt']; ?>" />
                     <p><?php echo $auth_name; ?></p>
                                <p><?php echo $auth_office ?></p>

                <?php $postid = get_the_ID(); ?> 
                <ul class="products">
                    <?php
                        $args = array(
                            'post_type' => 'product'
                            );
                        $loop = new WP_Query( $args );
                        if ( $loop->have_posts() ) {
                            while ( $loop->have_posts() ) : $loop->the_post(); ?>
                                <?php 
                                    $locations = get_field('custom_product_author');
                                    $to_obj = $locations[0];
                                    $to_id = $to_obj->ID;
                                    $to_id_str = (string)$to_id;
                                ?>
                                <?php if(  $postid == $to_id_str ): ?>
                                    <ul>
                                    <?php foreach( $locations as $location ): ?>
                                        <li>
                                            <?php wc_get_template_part( 'content', 'product' ); ?>
                                        </li>
                                    <?php endforeach; ?>
                                    </ul>
                                <?php endif; ?>   
                            <? endwhile;
                        } else {
                            echo __( 'No products found' );
                        }
                        wp_reset_postdata();
                    ?>
                </ul>
            <?php endwhile; // end of the loop. ?>

ACF | Querying relationship fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM