为什么如果我从 functions.php 中调用自定义函数它不起作用?
我的自定义功能:
function get_cat_slug($ID) {
$post_id = (int) $ID;
$category = &get_category($post_id);
echo $category->slug;
}
循环遍历所有帖子,如下所示:
$args = array(
'numberposts' => -1
);
$posts = get_posts($args);
foreach ($posts as $post){
// and withing this lopp I would like to get a category slug
// so I am calling my custom function, also included in functions.php
get_cat_slug($post->ID);
}
但是,get_cat_slug($post->ID)
总是返回null
。为什么?我错过了什么?任何建议都非常感谢。