爲什麼如果我從functions.php中調用自定義函數,它不起作用?WordPress的:使用functions.php和foreach循環
我的自定義函數:
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
。爲什麼?我錯過了什麼? 任何建議非常感謝。
更改後,我的函數仍然返回null。如何檢查函數是否正在調用? – Iladarsda
你在哪裏運行你的循環?你應該檢查你是否確實在get_cat_slug調用中傳遞了一個有效的職位ID。 'echo $ post-> ID',然後調用get_cat_slug函數。 – crowjonah
如果你看到空輸出,函數被調用 – crowjonah