get_template_part foreach 循环中并访问项的变量


get_template_part in a foreach loop and access to a var of item

我的代码有错误:

头版.php :

<?php $categories = get_categories( array( 'taxonomy' => 'category', 'parent' => 0, 'hide_empty' => 0 ) ); ?>
<?php get_template_part( 'templates/categories-list' ); ?>

分类列表.php :

<?php global $categories; ?><?php /* for get $categories from front-page.php */ ?>
<?php foreach ( $categories as $cat ) : ?>
    <?php get_template_part( 'templates/category-meta' ); ?>
<?php endforeach; ?>

类别元.php :

<?php global $cat; ?><?php /* for get $cat from categories-list.php */ ?>
<?php var_dump($cat); ?>

$cat是空的,为什么?怎么办?

我通过使用 include && locate_template 解决了我的问题

<?php include( locate_template( 'templates/category-meta.php' ) ); ?>