我想获得分类法“类型”中所有“带”的数组,然后在其他函数等中使用该数组utilities.php
文件,我require()
在里面functions.php
它有以下函数定义,并在之后立即调用:
function get_all_bands()
{
$bands = get_posts(array(
\'post_type\' => \'model\',
\'posts_per_page\' => -1,
// Includes the bands
// NOTE: this wont work if this function is called from functions.php
// which I need to do.
\'tax_query\' => array(
array(
\'taxonomy\' => \'types\',
\'field\' => \'slug\',
\'terms\' => \'band\',
\'operator\' => \'IN\'
)
)
));
return $bands;
}
print_r(get_all_bands())
如果我print_r(get_all_bands())
在模板中,如header.php
, page.php
, 或者一般来说,任何负责显示内容的模板,都可以正常工作。但如果我print_r(get_all_bands())
在里面functions.php
它返回一个空数组,所以我不能在任何东西上使用它。没有tax_query
它可以在任何地方工作:
// This works
function get_all_bands()
{
$bands = get_posts(array(
\'post_type\' => \'model\',
\'posts_per_page\' => -1
));
return $bands;
}
print_r(get_all_bands())
我还尝试添加global $post
但它什么也没做。编辑:明确定义和调用函数的位置。