所以我试图在链接到术语链接的列表项中仅显示5 个随机子术语。我很接近,但我似乎不能只显示 5。
这是我的代码:
<?php
$terms = get_terms( 'types' );
echo '<ul>';
// shuffle!
shuffle( $terms );
// slice the array
array_slice($terms, 0, 4);
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( 0 == $term->parent ) {
continue;
}
// We successfully got a link. Print it out.
echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?>
我尝试在随机播放后使用 array_slice
,但它似乎不起作用。
有什么建议吗?
最佳答案
只需将 array_slice
结果返回到一个变量中,例如像这样的 $terms
$terms = array_slice($terms, 0, 4);
var_dump($terms); //see you have 5 elements
关于php - 无法在 Wordpress 中仅显示 5 个术语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26697658/