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

Display aside title if custom post type is related to course ID matching current course page ID

我下面的当前代码始终显示标题 'course instructors',当我需要它在没有课程讲师的情况下消失时。

我需要发生的事情是:

您需要执行检查此课程是否有任何培训师的逻辑,然后您可以对结果执行任何检查。 此外,将您的逻辑移出 html 是个好主意。 Wordpress 并没有使这变得简单,但我们至少可以执行文件顶部的逻辑:

<?php
//lets get all our logic out of the html
$trainersArray = array(
    'post_type'      => 'trainers',
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC'
);
query_posts($trainersArray);

$trainers = get_posts( $trainersArray );

//an array to hold any trainers for this specific course
$courseTrainers = [];

foreach ( $trainers as $trainer ) {

    $trainer_courses = get_field('courses',$trainer->ID);  //SELECT THE CONNECTED COURSE'S CUSTOM FIELD

    if(!$trainer_courses){
        continue; //no need to carry on this iteration
    }

    $fullName = get_the_title($trainer->ID); //GET THE NAME FIELD IN TESTIMONIAL POSTS
    $trainerPage = get_the_permalink($trainer->ID);

    foreach( $trainer_courses as $trainer_course ){
        //i presume $Course_ID is set elsewhere but is in scope?
        if($trainer_course->ID == $Course_ID){
            $courseTrainers[] = [
                'fullName'=>$fullName,
                'pageLink'=>$trainerPage
            ];
            break; //no further iterations needed
        }
    }
}
wp_reset_query();
?>
<div class="instructors">
    <?php if(count($courseTrainers)>0)://an empty array is falsey,but this is more explicit?>

        <h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show -->

    <?php endif;?>

    <div id="trainers_list">
        <?php foreach($courseTrainers as $trainer):?>
            <div class="instructor-block">
                <div class="instructor-profile ">
                    <div class="profile-name">
                        <?php echo $trainer['fullName']; ?>
                    </div>
                    <div class="profile-link">
                        <a href="<?php echo $trainer['pageLink']; ?>">
                            View Full Profile
                        </a>
                    </div>
                </div>
            </div><!-- Your BR has been eradicated Feranndo - long live css ;-)-->
        <?php endforeach;?>

    </div><!-- .trainers_list -->
</div><!-- .instructors -->

以下是在主要问题变得清晰之前发现的一些其他问题,但仍然值得在答案中保留:

1:您正在使用较短的开放标签,这些标签已过时并且可能是问题所在,更改:

  <? if (!$trainers) { }else{?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><? } ?>
//^short tag                                                                                                                           ^another

至:

<?php if (!$trainers) { }else{?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><?php } ?>

2:双重否定很奇怪,改成

<?php if ($trainers){?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><?php } ?>

3:$trainers 直到检查后才声明,因此将该代码向上移动:

<?php 

    $trainersArray = array(
       'post_type'      => 'trainers',
       'posts_per_page' => -1,
       'orderby' => 'name',
       'order' => 'ASC'
    );
    query_posts($trainersArray);

    $trainers = get_posts( $trainersArray );

?>
<div class="instructors">
    <?php if ($trainers){?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><?php } ?>
<div id="trainers_list">
    <?php                         
    foreach ( $trainers as $trainer ) {...

5:最后,不要在if():endif;if(){} 语法之间切换,这会造成混淆。使用 html 时,首选前者,因此最终代码变为:

<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($trainers):?>

    <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 Feranndo? and it's not even in body text, its after a DIV, whats your problem? -->
                    <?php 
                    endif;

                endforeach; 

            endif; 

        endforeach; 
        wp_reset_query(); ?>

    </div><!-- .trainers_list -->
</div><!-- .instructors -->