如何在 div 中获取分类法
How to get taxonomy in a div
我想在我的 div 中添加其他不同字段的分类法,但我是 wordpress 的新手,我不知道该怎么做,如果有人可以帮助我吗?
<?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; ?>
这是一个代码,可以获取当前 post 的分类法,并将它们显示在列表中 <div><?php the_field('url')?></div>
[=12 下插入此代码=]
// 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).
}
我想在我的 div 中添加其他不同字段的分类法,但我是 wordpress 的新手,我不知道该怎么做,如果有人可以帮助我吗?
<?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; ?>
这是一个代码,可以获取当前 post 的分类法,并将它们显示在列表中 <div><?php the_field('url')?></div>
[=12 下插入此代码=]
// 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).
}