我目前正在构建一个Wordpress网站,我在以下方面遇到了一些困难。
我正在尝试通过显示当前帖子类型的自定义分类名称来动态地将类添加到 HTML 元素中,以用作类名称。这一切都是在 Foreach 循环中完成的。
我的代码如下
<?php
$args = array( 'posts_per_page' => -1, 'post_type' => 'staff', 'orderby' => 'menu_order',
'order' => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php $terms = wp_get_post_terms( $post_ID, 'department' ); ?>
<?php global $post; $terms = wp_get_post_terms( $post->ID, 'department'); ?>
<div class="grid-item <?php echo $term->slug; ?> ">
<div class="staff-box">
<?php the_post_thumbnail('staff-member'); ?>
<a href="<?php echo the_permalink(); ?>">
<p class="staff-title"><?php the_title(); ?></p>
<p class="staff-job-title"><?php the_field('staff-job-title'); ?></p>
</a>
</div>
</div>
<?php endforeach;
wp_reset_postdata();?>
这是使用 slug 工作的; ?>显示类名,但它只在每个类名上显示"兽医",而它应该在每个项目上显示相关部门......
希望这是有道理的。
非常感谢。
对于任何感兴趣的人,我现在已经使用以下命令解决了这个问题:
<?php $term_list = wp_get_post_terms($post->ID, 'department', array("fields" => "all")); ?>
并使用
<?php echo $term_list[0]->slug ; ?>
作为类名。
谢谢
你也可以通过这段代码来解决这个问题,把这段代码放在循环中,'portfolio_category'是自定义分类名称
$terms = get_the_terms( $post->ID, 'portfolio_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;