问题描述
我想在我的div中添加其他领域的分类法,但是我是wordpress的新手,如果有人可以帮助我,O找不到怎么做的吗?
<?PHP/* Template Name: Archive Projets */ ?>
<?PHP get_header(); ?>
<?PHP
$posts = get_posts(array(
'posts_per_page' => 4,'post_type' => 'projects'
));
if( $posts ): ?>
<?PHP foreach( $posts as $post ):
setup_postdata( $post );
?>
<div>
<a href="<?PHP the_permalink(); ?>"><?PHP the_title(); ?></a>
<div><?PHP the_field('url')?></div>
<div><?PHP the_field('')?></div> /* HERE I WANT TAXONOMY */
</div>
<?PHP endforeach; ?>
<?PHP wp_reset_postdata(); ?>
<?PHP endif; ?>
解决方法
下面的代码将获取当前帖子的分类法,并将其显示在 <div><?php the_field('url')?></div>
// Get all terms
$terms = get_the_terms( $post->ID,array( 'Taxonomy_name') );
// Dispaly the taxonomies,if there one or more.
foreach ( $terms as $term ) {
echo '<div>' . $term->name . '</div></br>';
}
确保使用您的分类名称更改“ Taxonomy_name”。如果我弄错了您或者是否需要代码方面的帮助,请告诉我。
,快速的Google搜索会弹出get_terms()。
$terms = get_terms( array(
'taxonomy' => 'post_tag','hide_empty' => false,) );
foreach($terms as $current_term) {
//You can now loop through them and get the ones you want,display them all,or whatever else it is you want to do. Note: each $current_term is an object of type WP_Term (or error if there are no results).
}