简体   繁体   中英

ACF Repeater Display row fields if field_name = page_name

I've use Advanced Custom Fields to fill in contact details for 6 Schools - which all show on the Contact page.

Each school has a course page and I would like to display those contact details on the course page for that school.

I need school A's contact details to show on the "Courses at school A" page. The course page name "School A" is identical to the get_sub_field('school_name').

    <?php if(have_rows('schools', 45) ): ?> // 45 is post ID in WP
    <?php  while (have_rows('schools', 45) ): the_row(); 

    // vars
    $pagetitle = get_the_title(); // get page title in WP
    $school = get_sub_field('school_name'); // get school name

    if ($school == $pagetitle){ // IF they are the same THEN
    ?> 

    <article class="school-contacts">
    <ul class="contact-info">
        <li><strong><?php echo $school; ?></strong></li>
//other fields omitted for clarity
       </ul> 
    </article> 
    <?php } ?>
    <?php endwhile; endif; ?>

My problem is that ALL the schools show up when I do this.

I need to find a way so that only School A shows up on School A page, School B on School B page etc.

I've added in some checks (removed from this code for clarity) but they show that School = School A Pagetitle = School A So the matching is working correctly.

I'm just stuck on how I can get the ACF output to ONLY show school A.

Solved it ... always the simplest things:

    <?php if(have_rows('schools', 45) ):
      while (have_rows('schools', 45) ): the_row();

    // vars
    $pagetitle = get_the_title();
    $school = get_sub_field('school_name');
    $logo = get_sub_field('logo');
    $contact = get_sub_field('contact_name');
    $title = get_sub_field('contact_title');
    $email = get_sub_field('contact_email');
    $website = get_sub_field('contact_website');
    $phone = get_sub_field('contact_phone');    
    ?>

    <?php if ($school == $pagetitle) : the_row(); ?>
// needed : the_row(); to output the result

    <article class="school-contacts">

    <div class="school-logo"><img src="<?php echo $logo; ?>" width="100%" /></div>
    <ul class="contact-info">
        <li><strong><?php echo $school; ?></strong></li>
        <li><?php echo  $contact; ?></li>
        <li><i><?php echo  $title; ?></i></li>
                    <li><?php echo  $phone; ?></li>
        <li><a href="mailto:<?php echo  $email; ?>"><?php echo  $email; ?></a></li>
        <li><a href="<?php echo  $website; ?>"><?php echo  $website; ?></a></li>

    </ul> 
    </article> 
    <?php endif;  endwhile;  endif; ?>

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