通过自定义插件创建分类法时如何创建分类法术语?

时间:2018-09-04 21:49:30

标签: wordpress custom-post-type taxonomy-terms

我正在尝试在创建分类法时实用地添加分类法术语。我尝试添加

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {BehaviorSubject} from 'rxjs';
import {map} from 'rxjs/operators';
import {JwtHelperService} from '@auth0/angular-jwt';
import { environment } from '../../environments/environment';
import { User } from '../_models/user';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  baseUrl = environment.apiUrl + 'auth/';
  jwtHelper = new JwtHelperService();
  decodedToken: any;
  currentUser: User;
  photoUrl = new BehaviorSubject<string>('../../assets/user.png');
  currentPhotoUrl = this.photoUrl.asObservable();
  this.changeMemberPhoto(this.currentUser.photoUrl);

constructor(private http: HttpClient) { }

changeMemberPhoto(photoUrl: string) {
  this.photoUrl.next(photoUrl); 
}

login(model: any) {
  return this.http.post(this.baseUrl + 'login', model).pipe(
    map((response: any) => {
      const user = response;
      if (user) {
        localStorage.setItem('token', user.token);
        localStorage.setItem('user', JSON.stringify(user.user));
        this.decodedToken =  this.jwtHelper.decodeToken(user.token);
        this.currentUser=user.user;
        this.changeMemberPhoto(this.currentUser.photoUrl);
      }
    })
  );
}

register (model: any) {
  return this.http.post(this.baseUrl + 'register', model);
}

loggedIn() {
  const token = localStorage.getItem('token');
  return !this.jwtHelper.isTokenExpired(token);
}

}

wp_insert_term( 'A00', // the term 'we_colors' ); ,但这并未添加术语tax_color_palletes。我正在编写此代码段;我该如何解决?

A00

1 个答案:

答案 0 :(得分:0)

您是否要将term添加到taxonomypost_type中?

在您的示例中,您将taxonomy'women_models'注册到了post_type'we_colors'中。 但是随后您调用wp_insert_term(需要taxonomy)并将其传递给post_type。哪个应该给你一个错误。

如果仅要将term添加到taxonomy,则需要将taxonomy传递到wp_insert_term

wp_insert_term('A00', 'women_models');

如果相反,您实际上是想在post_type中添加术语,则应使用wp_set_object_terms,后者可以依次调用wp_insert_term来创建新术语。不过,您需要先获取$object_id

wp_set_object_terms( $object_id, ['term_1', 'term_2'], 'taxonomy_name');
  

https://developer.wordpress.org/reference/functions/register_taxonomy/   https://developer.wordpress.org/reference/functions/wp_insert_term/   https://developer.wordpress.org/reference/functions/wp_set_object_terms/