Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress自定义分类术语元值_Php_Wordpress_Taxonomy Terms - Fatal编程技术网

Php WordPress自定义分类术语元值

Php WordPress自定义分类术语元值,php,wordpress,taxonomy-terms,Php,Wordpress,Taxonomy Terms,我已经为事件类别创建了自定义分类法 我在类别中添加了一个颜色选择器表单字段,这样所有添加的类别都可以有一个与之关联的颜色 我在数据库中的我的wp_termeta表中保存了数据: meta_id = 1 term_id = 2 meta_key = _category_color meta_value = 8224e3 (hex color) 我正在fullcalendar中显示事件,并尝试获取事件分类的颜色,并将其用作日历中的事件背景颜色 我正在为我的JSON提要使用一个php文件,并且已经

我已经为事件类别创建了自定义分类法

我在类别中添加了一个颜色选择器表单字段,这样所有添加的类别都可以有一个与之关联的颜色

我在数据库中的我的wp_termeta表中保存了数据:

meta_id = 1
term_id = 2 
meta_key = _category_color
meta_value = 8224e3 (hex color)
我正在fullcalendar中显示事件,并尝试获取事件分类的颜色,并将其用作日历中的事件背景颜色

我正在为我的JSON提要使用一个php文件,并且已经在WP_查询中完成了所有事件并循环它们,但是我不知道如何从术语_meta中获取_category_color值

$events = array();
$result = new WP_Query('post_type=event');

foreach($result->posts as $post) {

// To get the term ID I tried
$term = get_the_terms( $post->ID, 'eventcategory' );
$term_vals = get_term_meta($term,'_category_color');

$events = array(
    'title'   => $post->post_title,
    'start'   => date( 'Y-m-d', $data['start-date'] ),
    'end'     => date( 'Y-m-d', $data['end-date'] ),
    'url'   =>  get_post_permalink($post->ID),
    'backgroundColor' => '#'.$term_vals["_category_color"][0],
    'allDay'  => false
    );

}

echo json_encode($events);
exit;

我只是把代码弄得一团糟,想不出怎么做才对?

我已经想出来了

我是如此接近,然而,到目前为止。我已经对代码进行了注释,让您知道我是如何理解的

$events = array();
$result = new WP_Query('post_type=event');

foreach($result->posts as $post) {

// I have to first get the Terms
$terms = get_the_terms( $post->ID, 'choirboss_eventcategory' );

//Then I had to loop through the terms to get the term_id
if($terms) {
    foreach( $terms as $term ) {
        $term_id = $term->term_id;
    }
}

//Now I can grab my _category_color like this
$category_color = get_term_meta($term_id, '_category_color', true);

$events = array(
    'title'   => $post->post_title,
    'start'   => date( 'Y-m-d', $data['start-date'] ),
    'end'     => date( 'Y-m-d', $data['end-date'] ),
    'url'   =>  get_post_permalink($post->ID),
    'backgroundColor' => '#'.$category_color,
    'allDay'  => false
    );

}

echo json_encode($events);
exit;