【问题标题】:WordPress Custom Taxonomy Term Meta ValueWordPress 自定义分类术语元值
【发布时间】:2020-01-04 23:25:50
【问题描述】:

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

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

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

meta_id = 1
term_id = 2 
meta_key = _category_color
meta_value = 8224e3 (hex color)

我正在全日历中显示事件,我正在尝试获取事件分类的颜色并将其用作日历中的事件背景颜色。

我正在为我的 JSON 提要使用 php 文件,并且已经在 WP_Query 中通过并循环了所有事件,但我不知道如何从 term_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;

我只是把代码弄得一团糟,不知道该怎么做?

【问题讨论】:

    标签: php wordpress taxonomy-terms


    【解决方案1】:

    我已经想通了。

    我是如此接近,然而,如此遥远。我已经对代码进行了注释,让您知道我是如何计算出来的。

    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 2012-07-11
      • 2012-08-09
      • 2016-10-01
      • 1970-01-01
      相关资源
      最近更新 更多