在教育课程数据库中,学院被用作自定义分类法。如何在所有相关帖子中显示“学院”分类法的描述,因为一个学院至少有50个帖子(课程)。。。
显示预设的分类说明
2 个回复
SO网友:Chip Bennett
假设:
institute
是分类学course
术语是否只有一个course
根据要显示的帖子分配course description?get_the_terms()
. 如果您的分类是institute
, 您可以执行以下操作:
如果我的假设不正确,请在评论中告诉我,我会更新答案。<?php // globalize $post global $post; // Get the course for the current post $post_courses = get_the_terms( $post->ID, \'institute\' ); // Assuming only one course, get the first object of the array $post_course = $post_courses[0]; // Get the course description $course_description = $post_course->description; // Output the course description echo $course_description ?>
EDIT
其他假设没问题,但我想显示“机构描述”。每个职位只分配一门课程,但每个学院都有几门课程。Institute是分类学,institutes的名称用作类别。我仍然不能完全确定我是否在跟踪你。你是这么说的吗
institute
和course
是分级分类法吗?如果是这样,那么我最初的假设是错误的。Can you more clearly describe your schema?EDIT 2
在本例中,我想显示奥克兰大学的描述。而这所大学也被用于许多其他岗位。就我而言,每门课程都是一篇帖子。好了,现在我们有进展了!看来
course
是一个Custom Post Type, 还有那个institute
是一个Custom Taxonomy 对于该CPT。如果这是正确的,那么我上面的代码应该可以用来显示给定帖子的术语描述。
SO网友:endle.winters
下面是我最近用来显示自定义分类法(称为服务类型)中的每个术语的代码,以及描述,然后在下面显示它下面的所有自定义post类型(服务)。
我添加了一个if语句,这样如果Service\\u类型没有描述,它就不会为其创建div(因此没有很大的空白)。您可以修改它以显示特定的自定义分类法。这至少会让你开始。我还做了一些其他页面来显示分类法中的特定术语,因此如果您需要更多信息,请写出描述。
<?php
//get all service_types (custom taxonomy) then display all posts in each term
$taxonomy = \'service_types\';
$term_args=array(
\'orderby\' => \'menu_order\',
\'order\' => \'DESC\'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
\'post_type\' => \'services\',
\'service_types\' => $term->name ,
\'post_status\' => \'publish\',
\'posts_per_page\' => 50,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<div class="services"> <!--begin this type of service -->
<div class="description">
<h4><?php echo $term->name;?></h4>
<?php
$termDiscription = $term->description;
if($termDiscription != \'\') : ?>
<p><?php echo $termDiscription; ?></p>
<?php endif; ?>
</div> <!--.description -->
<?php
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<!--begin this service -->
<div class="cpt service">
<h5><?php the_title(); ?></h5><!--title -->
<!--thumbnail -->
<span class="sthumb">
<a href="<?php the_permalink() ?>" title="<?php echo $term->name;?>"><?php the_post_thumbnail(); ?></a>
</span>
<!--#thumb -->
<?php the_excerpt(); ?>
</div><!--#cpt -->
<?php endwhile; ?>
结束