我目前的代码始终显示标题“课程教师”,当我需要它时,如果没有课程教师,它就会消失。
我需要做的是:
如果任何培训师的课程编号与当前课程页面ID相匹配,则显示标题为“课程教师”。
<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 -->
答案 0 :(得分:2)
您需要执行逻辑,检查此课程是否有任何培训师 之前您可以对结果执行任何检查。 此外,将你的逻辑移出你的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 -->