Php 获取分类Slug
我使用以下代码获取分类slug:Php 获取分类Slug,php,wordpress,Php,Wordpress,我使用以下代码获取分类slug: <?php $terms = get_the_terms( $post->ID, 'locations' ); if ( !empty( $terms ) ){ $term = array_shift( $terms ); } ?> 然后,我使用以下代码输出slug: <?php echo $term->slug; ?> 我的问题是,如何使用它在同一位置输出两个不同的分类
<?php
$terms = get_the_terms( $post->ID, 'locations' );
if ( !empty( $terms ) ){
$term = array_shift( $terms );
}
?>
然后,我使用以下代码输出slug:
<?php echo $term->slug; ?>
我的问题是,如何使用它在同一位置输出两个不同的分类法?例如:
<?php
$terms = get_the_terms( $post->ID, 'locations', 'status' );
if ( !empty( $terms ) ){
$term = array_shift( $terms );
}
?>
我想我可能可以添加术语“location”、“status”,但它不起作用。如果您想显示两个或更多分类法,那么我认为您应该循环$terms变量
<?php
$terms = get_the_terms( $post->ID, 'locations' );
if ( !empty( $terms ) ){
foreach ($terms as $term):
echo $term->slug;
endforeach;
}
?>
希望它能帮助你
谢谢如果您想显示两个或更多分类法,那么我认为您应该循环$terms变量
<?php
$terms = get_the_terms( $post->ID, 'locations' );
if ( !empty( $terms ) ){
foreach ($terms as $term):
echo $term->slug;
endforeach;
}
?>
希望它能帮助你
谢谢根据的官方文档,只能提供一种分类法。如果您想输出两种不同分类法中所有术语的slug,可以按照Mohammad的建议执行,但要执行两次 i、 e
根据的官方文档,只能提供一种分类法。如果您想输出两种不同分类法中所有术语的slug,可以按照Mohammad的建议执行,但要执行两次 i、 e
谢谢你的评论。我已经更新了我的答案,让它更清楚。我正在尝试使用上面的代码输出两种不同的分类法。这真的取决于您想要输出什么?我想要输出分类法slug。谢谢您的评论。我已经更新了我的答案,让它更清楚。我正试图使用上面的代码输出两种不同的分类法。这真的取决于您想要输出什么?我想要输出分类法slug。
<?php
$loc_field = 'name';
$loc_field_value = 'special location';
$loc_taxonomy = 'locations';
$locations_term = get_term_by( $loc_field, $loc_field_value, $loc_taxonomy );
echo $locations_term->slug;
$stat_field = 'name';
$stat_field_value = 'special status';
$stat_taxonomy = 'status';
$status_term = get_term_by( $stat_field, $stat_field_value, $stat_taxonomy );
echo $status_term->slug;
?>