php - 无法在 Wordpress 中仅显示 5 个术语?

标签 php html css wordpress slice

所以我试图在链接到术语链接的列表项中仅显示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/

相关文章:

php - paypal商户SDK安装方法

php - 触发器根据另一个表更新计数

PHP边阅读边下载文件

javascript - 为什么我的更改按钮不起作用?

html - float :left paragraph followed by float:none paragraph

html - CSS 响应表 - 高度问题

php - MySQL 类似函数

javascript - 必需的属性在 Firefox 上不起作用

html - 在 CSS 网格中对齐表单标签?

html - moz 选择器在 CSS 中不起作用