如果自定义帖子类型的相关课程ID与当前课程页面ID匹配,则显示标题

My current code below display the heading 'course instructors' at all times, when I need it to disappear when there are no course instructors.

What I need to happen is:

  • Query all 'trainers' posts.
  • Query the 'courses' field within each post.
  • Check to see if the current course page ID exists within the 'courses' field within any of the trainers posts.
  • Display the trainers who have a course ID that matches the current course page ID.
  • If any trainers has course ID that matches current course page ID then display heading 'Course Instructor'.

                <div class="instructors">
    
                    <?php
                    $trainersArray = array(
                        'post_type'      => 'trainers',
                        'posts_per_page' => -1,
                        'orderby' => 'name',
                        'order' => 'ASC'
                    );
                    query_posts($trainersArray);
    
                    $trainers = get_posts( $trainersArray );
    
                    ?>
    
                    <?php /*if($Course_ID):*/?>
    
                        <h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show -->
    
                    <?php /*endif;*/?>
    
                    <div id="trainers_list">
                        <?php
    
                        foreach ( $trainers as $trainer ) :
    
                            $trainerID = $trainer->ID;
                            $trainer_courses = get_field('courses',$trainerID);  //SELECT THE CONNECTED COURSE'S CUSTOM FIELD
                            $fullName = get_the_title($trainerID); //GET THE NAME FIELD IN TESTIMONIAL POSTS
                            $trainerPage = get_the_permalink($trainerID);
                            $feedback_count = 0;
    
                            if( $trainer_courses ):
    
                                foreach( $trainer_courses as $trainer_course ):
    
                                    $trainerCourseID = $trainer_course->ID;
    
                                    if ($trainerCourseID == $Course_ID) : ?>
    
                                        <div class="instructor-block">
                                            <div class="instructor-profile ">
                                                <div class="profile-name">
                                                    <?php echo $fullName; ?>
                                                </div>
                                                <div class="profile-link">
                                                    <a href="<?php echo $trainerPage; ?>">
                                                        View Full Profile
                                                    </a>
                                                </div>
                                            </div>
                                        </div><br><!-- Another BR Fernando? and it's not even in body text, its after a DIV, whats your problem? -->
                                    <?php 
                                    endif;
    
                                endforeach; 
    
                            endif; 
    
                        endforeach; ?>  <!--echo json_encode( $trainers ); -->
    
                        <? wp_reset_query(); ?>
    
                    </div><!-- .trainers_list -->
                </div><!-- .instructors -->
    

"If any trainers has course ID that matches current course page ID then display heading 'Course Instructor'".

Well you have to find out if any trainers have a course ID which matches the current course page ID before you decide if you display the heading obviously.

You may either do the foreach-loop twice or you just make an array containing $fullName and $trainerPage.

If the array contains elements you display the heading and make a simple foreach loop over the new array where you just do the output of the previously gained information about course instructors.

If I did not get the problem correctly please comment why.