在foreach循环中获取get_template_part并访问var的项目

时间:2016-03-09 08:31:27

标签: php wordpress

我对此代码有错误:

front-page.php:

<?php $categories = get_categories( array( 'taxonomy' => 'category', 'parent' => 0, 'hide_empty' => 0 ) ); ?>

<?php get_template_part( 'templates/categories-list' ); ?>

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; ?>

category-meta.php:

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

<?php var_dump($cat); ?>

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

1 个答案:

答案 0 :(得分:3)

我使用include&amp;&amp ;;解决了我的问题locate_template

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