自定义分类术语未按顺序打印
Custom taxonomy terms not printing in order
我在自定义 post 类型中有多个自定义分类法。我正在循环打印自定义 posts 及其多个分类法。我能做到。但是分类法是按这样的随机顺序打印的:
这是循环打印的内容:
你可以看到一些工作类型的数据在公司字段打印,一些公司在技能字段打印。我怎样才能按顺序打印它们。
这是我的代码:
$args=array("post_per_page"=>-1,"post_type"=>"jobs");
$loop=new WP_Query($args);
if($loop->have_posts()){
while($loop->have_posts()):$loop->the_post();
$custom_terms=wp_get_object_terms(get_the_ID(),array('companies','job-type','skills'),array('orderby' => 'name', 'order'=>'ASC','fields'=>'all'));?>
<div class="col-md-12" style="box-shadow:0px 3px 5px 2px #f4f4f4;margin:10px;background:#fff;">
<div class="media col-md-3" style="margin-top:2%;">
<figure class="pull-left">
<?php the_post_thumbnail('small',array('class'=>'media-object img-rounded img-responsive'));?>
<h5 class="list-group-item-heading" style="font-weight: 400;color: #0db294;margin-bottom:3%;border-bottom:1px solid rgb(98,59,204,0.1);padding:5px;"><?php echo the_title();?> </h5>
</figure>
</div>
<div class="col-md-6" style="margin-top:3%;">
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Company</label><br/><span style="text-transform:capitalize"><?php echo $custom_terms[1]->name;?></span></div>
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Job Type</label><br/><?php echo $custom_terms[0]->name;?></div>
<div class="col-md-4 col-xs-12 text-center"><label style="color: #0db294;">Skills</label><br/><?php echo $custom_terms[2]->name;?></div>
</div>
<div class="col-md-3 col-xs-12 text-center"><br/>
<a href="<?php echo the_permalink();?>" class="btn btn-primary btn-block offer-btn">View Details</a>
</div>
</div>
<?php endwhile;
}
使用get_the_term_list获取特定分类的所有术语。如果有多个,您也可以修改它以获得第一个。
点赞这个ID, 'companies', 'Company: ', ', ' ); ?>
您正在获取所有内容并按名称排序然后按位置获取。这导致了问题。像这样手动获取每一个。
这是@boris 建议的解决方案
$args=array("post_per_page"=>-1,"post_type"=>"jobs");
$loop=new WP_Query($args);
if($loop->have_posts()){
while($loop->have_posts()):$loop->the_post();
$comp=get_the_term_list(get_the_ID(),'companies','',',');
$jobt=get_the_term_list(get_the_ID(),'job-type','',',');
$skil=get_the_term_list(get_the_ID(),'skills','',',');
?>
<div class="col-md-12 col-xs-12" style="margin:10px;background:#fff;margin-left:0;">
<div class="media col-md-4 col-xs-12" style="margin-top:2%;">
<figure class="pull-left">
<!-- <img class="media-object img-rounded img-responsive" src="" alt="placehold.it/350x250" style="height:70px;width:auto;margin:0 auto" > -->
<h2 class="list-group-item-heading" style="font-weight: 400;font-size:19px;color: #2A5A8E;margin-bottom:3%;padding:5px;"><?php echo the_title();?> </h2>
</figure>
</div>
<div class="col-md-6" style="margin-top:3%;">
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Company</label><br/><span style="text-transform:capitalize"><?php echo $comp;?></span></div>
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Job Type</label><br/><?php echo $jobt;?></div>
<div class="col-md-4 col-xs-12 text-center"><label style="color: #0db294;">Skills</label><br/><?php echo $skil;?></div>
</div>
<div class="col-md-2 col-xs-12 text-center"><br/>
<a href="<?php echo the_permalink();?>" class="btn btn-warning btn-block offer-btn">View Details</a>
</div>
</div>
<?php endwhile;
}
?>
我在自定义 post 类型中有多个自定义分类法。我正在循环打印自定义 posts 及其多个分类法。我能做到。但是分类法是按这样的随机顺序打印的:
这是循环打印的内容:
你可以看到一些工作类型的数据在公司字段打印,一些公司在技能字段打印。我怎样才能按顺序打印它们。
这是我的代码:
$args=array("post_per_page"=>-1,"post_type"=>"jobs");
$loop=new WP_Query($args);
if($loop->have_posts()){
while($loop->have_posts()):$loop->the_post();
$custom_terms=wp_get_object_terms(get_the_ID(),array('companies','job-type','skills'),array('orderby' => 'name', 'order'=>'ASC','fields'=>'all'));?>
<div class="col-md-12" style="box-shadow:0px 3px 5px 2px #f4f4f4;margin:10px;background:#fff;">
<div class="media col-md-3" style="margin-top:2%;">
<figure class="pull-left">
<?php the_post_thumbnail('small',array('class'=>'media-object img-rounded img-responsive'));?>
<h5 class="list-group-item-heading" style="font-weight: 400;color: #0db294;margin-bottom:3%;border-bottom:1px solid rgb(98,59,204,0.1);padding:5px;"><?php echo the_title();?> </h5>
</figure>
</div>
<div class="col-md-6" style="margin-top:3%;">
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Company</label><br/><span style="text-transform:capitalize"><?php echo $custom_terms[1]->name;?></span></div>
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Job Type</label><br/><?php echo $custom_terms[0]->name;?></div>
<div class="col-md-4 col-xs-12 text-center"><label style="color: #0db294;">Skills</label><br/><?php echo $custom_terms[2]->name;?></div>
</div>
<div class="col-md-3 col-xs-12 text-center"><br/>
<a href="<?php echo the_permalink();?>" class="btn btn-primary btn-block offer-btn">View Details</a>
</div>
</div>
<?php endwhile;
}
使用get_the_term_list获取特定分类的所有术语。如果有多个,您也可以修改它以获得第一个。
点赞这个ID, 'companies', 'Company: ', ', ' ); ?>
您正在获取所有内容并按名称排序然后按位置获取。这导致了问题。像这样手动获取每一个。
这是@boris 建议的解决方案
$args=array("post_per_page"=>-1,"post_type"=>"jobs");
$loop=new WP_Query($args);
if($loop->have_posts()){
while($loop->have_posts()):$loop->the_post();
$comp=get_the_term_list(get_the_ID(),'companies','',',');
$jobt=get_the_term_list(get_the_ID(),'job-type','',',');
$skil=get_the_term_list(get_the_ID(),'skills','',',');
?>
<div class="col-md-12 col-xs-12" style="margin:10px;background:#fff;margin-left:0;">
<div class="media col-md-4 col-xs-12" style="margin-top:2%;">
<figure class="pull-left">
<!-- <img class="media-object img-rounded img-responsive" src="" alt="placehold.it/350x250" style="height:70px;width:auto;margin:0 auto" > -->
<h2 class="list-group-item-heading" style="font-weight: 400;font-size:19px;color: #2A5A8E;margin-bottom:3%;padding:5px;"><?php echo the_title();?> </h2>
</figure>
</div>
<div class="col-md-6" style="margin-top:3%;">
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Company</label><br/><span style="text-transform:capitalize"><?php echo $comp;?></span></div>
<div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Job Type</label><br/><?php echo $jobt;?></div>
<div class="col-md-4 col-xs-12 text-center"><label style="color: #0db294;">Skills</label><br/><?php echo $skil;?></div>
</div>
<div class="col-md-2 col-xs-12 text-center"><br/>
<a href="<?php echo the_permalink();?>" class="btn btn-warning btn-block offer-btn">View Details</a>
</div>
</div>
<?php endwhile;
}
?>