Get related terms by parent term with REST API
PHP
if(!class_exists('ODS_REST_API')){ /** * 取得圖片主分類的關聯分類 * * 範例:取得國家 term id 773 的關聯地區 * https://example.com/wp-json/ods/v1/area/773 */ class ODS_REST_API { private $related_area_data; /** * */ public function get_temrs_by_related( $request ) { $related_area = get_field( 'country_related_area', $request['tax'].'_tax_'.$request['id']); foreach($related_area as $data) { $this->related_area_data[] = [ 'term_id' => $data->term_id, 'name' => $data->name ]; } if ( $related_area ) { return rest_ensure_response( $this->related_area_data ); } else { return new WP_Error( 'rest_product_invalid', 'The term does not exist.', array( 'status' => 404 ) ); } return new WP_Error( 'rest_api_sad', 'Something went horribly wrong.', array( 'status' => 500 ) ); } /** * */ public function register_terms_route() { register_rest_route( 'ods/v1', '/(?<tax>[a-z]+)/(?P<id>[\d]+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array($this,'get_temrs_by_related'), )); } } } $term_route = new ODS_REST_API(); add_action( 'rest_api_init', array( $term_route, 'register_terms_route') );
Tags:
- register_rest_route
- /
- rest-api
- /
- taxonomy
- /
- term
- /
Send variables to template
// in archive, taxonomy <?php $tax = 'news_tax'; ?> <ul class="uk-margin-medium-bottom uk-margin-medium-top uk-grid-match [email protected] [email protected] uk-grid-small" uk-grid=""> <?php while($i->have_posts()): $i->the_post(); ?> <?php if($i->current_post > 0): ?> <!-- <loopCardPrimary>--> <?php set_query_var('tax', $tax); ?> <?php echo get_template_part('partials/loops/card-primary'); ?> <!-- </loopCardPrimary>--> <?php endif; ?> <?php endwhile; wp_reset_postdata(); ?> </ul> // in loop template <?php $tax = get_query_var('tax'); ?>
Register custom taxonomy in array
PHP
function add_post_taxonomy() { $taxArray = array( array( "taxName" => 'tax中文名', "taxNameEn" =>'taxSlu' ), ); foreach ($taxArray as $tax) { $labels = array( "name" => __( "", "" ), "singular_name" => __( $tax['taxName'], "" ), "menu_name" => __( $tax['taxName'], "" ), "all_items" => __( "所有", "" ), "edit_item" => __( "編輯", "" ), "view_item" => __( "檢視", "" ), "update_item" => __( "更新", "" ), "add_new_item" => __( "新增", "" ), "new_item_name" => __( "新增", "" ), "search_items" => __( "搜尋", "" ), ); $args = array( "label" => __( $tax['taxName'], "" ), "labels" => $labels, "public" => true, "hierarchical" => true, "label" => $tax['taxName'], "show_ui" => true, "show_in_menu" => true, "show_in_nav_menus" => true, "show_admin_column" => true, "query_var" => true, "rewrite" => array( 'slug' => $tax['taxNameEn'], 'with_front' => true, ), "show_admin_column" => true, "show_in_rest" => false, "rest_base" => $tax['taxNameEn'], "show_in_quick_edit" => true, ); register_taxonomy( $tax['taxNameEn'], 'post', $args ); } } add_action( 'init', 'add_post_taxonomy' );
Get current term
PHP
global $wp_query; $term = $wp_query->get_queried_object();
Query the taxonomy terms by ACF field
PHP
<?php $args = array( 'taxonomy' => 'category', 'hide_empty' => false ); $cats = get_categories($args); $cats_has_feature = array(); foreach($cats as $cat){ if (get_field('term_is_feature', 'category_'.$cat->term_id)) { $cats_has_feature[] = $cat; // save the match term in Array } }
Tags:
- acf
- /