我无法正确显示循环类别。只需要在点亮的帖子中显示一个类别。为什么第一篇文章获得所有选定类别,而下一篇文章为何已正确显示一个类别。如何只在第一篇文章中显示当前选择的类别?我的代码如下所示。我附上了图片。
我的循环自定义帖子
<?php
/* Start the Loop */
$args = array( 'post_type' => 'database',
'posts_per_page' => 10,
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'order' => 'DESC',
'orderby' => 'date');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
这是在循环发布中显示一个类别
<?php
// Get terms for post
$terms = wp_get_post_terms( $post->ID , 'database_categories' ,$args2);
$args2 = array( 'parent' => 39,
'fields' => 'all');
// Loop over each item since it's an array
if ( $terms != null ) {
foreach( $terms as $term ) {
$term_link = get_term_link( $term, 'database_categories' );
// Print the name and URL
echo '<a href="' . $term_link . '">' . $term->name . '</a> ';
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
答案 0 :(得分:0)
如果这里的对象是获取第一个术语而忽略其余术语,那么我将移开第一个数组元素,而无需在术语上循环。
$args2 = array('parent' => 39, 'fields' => 'all');
$terms = wp_get_post_terms( $post->ID , 'database_categories' ,$args2);
if (!empty($terms)) {
$term = array_shift($terms);
echo sprintf('<a href="%s">%s</a>', get_term_link( $term, 'database_categories' ), $term->name);
}