【问题标题】:Get categories query not working in function.php获取类别查询在function.php中不起作用
【发布时间】:2015-07-24 11:10:56
【问题描述】:

我试图在 functions.php 中获取我的自定义帖子类型的类别,但它不返回任何值,当我在任何主题文件中运行此查询时,它工作正常。这是我的代码

function get_destinations(){
   $args = array(
    'type'                     => 'accomodation',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'facilitie',
    'pad_counts'               => false 

); 
 $categories = get_categories($args);$destinations = array();
              foreach ($categories as $cat) { 
                if($cat->cat_name != ''){
                    $destinations[$cat->cat_name] = $cat->cat_name;
                }
          }
return $destinations;
 }

我正在使用this 代码添加元字段,现在我必须通过类别来选择标签,即

$my_meta2->addSelect($prefix.'select_field_id',get_destinations(),array('name'=> 'Select Destination'));

原来的代码是这样的,他们在数组中传递值。

$my_meta->addSelect($prefix.'select_field_id',array('selectkey1'=>'Select Value1','selectkey2'=>'Select Value2'),array('name'=> 'My select ', 'std'=> array('selectkey2')));

但没有得到任何价值,知道我哪里错了。谢谢

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    据我所知,get_categories() 不会接受 post_type 参数。您可能应该为此使用分类法。

    您可以在此处找到更多信息: https://codex.wordpress.org/Function_Reference/get_categories

    【讨论】:

    • 阅读您发布的链接,它有 post_type,我使用 wordpress 查询,正如我所说,它在所有页面上都可以正常工作,除了在管理端显示数据的 functions.php。
    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 2021-12-01
    • 1970-01-01
    • 2023-03-11
    • 2017-07-17
    • 1970-01-01
    • 2022-01-11
    • 2013-05-05
    相关资源
    最近更新 更多