Php 如何使用自定义模板限制wordpress中显示的类别数?

Php 如何使用自定义模板限制wordpress中显示的类别数?,php,wordpress,Php,Wordpress,自定义模板中的代码是 $categories = get_categories( array( 'hide_empty' => 0 ) ); foreach ($categories as $cat) : ?> <div class="fusion-one-third fusion-layout-column fusion-spacing-yes"> <a href="<?php echo get_category_link($cat-

自定义模板中的代码是
$categories = get_categories( array(
'hide_empty'       => 0
) );

foreach ($categories as $cat) : ?>
<div class="fusion-one-third fusion-layout-column fusion-spacing-yes">

    <a href="<?php echo get_category_link($cat->term_id); ?>">
        <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
        <h1 class="customp"><?php echo $cat->cat_name; ?></h1>
    </a>
</div>
<?php endforeach; ?>
$categories=获取类别(数组)(
“hide_empty”=>0
) );
foreach(类别为$cat):?>
它显示创建的每个类别,但我只想显示6个最新类别。无论这些类别是否有帖子。

我认为在中,您可以使用
offset
参数来限制类别的数量,使用
orderby
order
参数来获取最新的类别:

$categories = get_terms( 'category', array(
    'hide_empty' => 0,
    'offset' => 6,
    'orderby' => 'id',
    'order' => 'DESC'
));

我不认为有任何限制选项,你可以设置与该功能

作为替代方案,您可以使用PHP函数对其进行切片:

$categories = get_categories( array(
'hide_empty'       => 0
) );

$number_of_categories = 6 // Whatever you want to 
$categories = array_slice($categories, 0, $number_of_categories, true);

foreach ($categories as $cat) : ?>
<div class="fusion-one-third fusion-layout-column fusion-spacing-yes">

    <a href="<?php echo get_category_link($cat->term_id); ?>">
        <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
        <h1 class="customp"><?php echo $cat->cat_name; ?></h1>
    </a>
</div>
<?php endforeach; ?>
$categories=获取类别(数组)(
“hide_empty”=>0
) );
$number\u of_categories=6//任何您想要的
$categories=array_slice($categories,0,$number_of_categories,true);
foreach(类别为$cat):?>

试试看

谢谢大家,我使用了计数器,它工作得非常好。
$categories = get_categories( array(
    'hide_empty'       => 0,
    'parent'  => 0
) );
$limit=6;
$counter=0;
foreach ($categories as $cat) : 
if($counter<$limit){

    ?>
<div class="fusion-one-third fusion-layout-column fusion-spacing-yes">

    <a href="<?php echo get_category_link($cat->term_id); ?>">
        <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
        <h1 class="customp"><?php echo $cat->cat_name; ?></h1>
    </a>
</div>
<?php $counter++;
                }
 endforeach; ?>
$categories=获取类别(数组)(
“hide_empty”=>0,
“父项”=>0
) );
$limit=6;
$counter=0;
foreach(类别为$cat):
如果($柜台
请参见以下内容:这将显示get\u categories中的数组接受哪些筛选器/值。我不确定您是否可以按最近创建的类别进行筛选。。。