REST API를 통해 워드프레스 메뉴 항목에 액세스하는 방법은 무엇입니까?
워드프레스의 주요 메뉴 항목을 다른 웹 애플리케이션에 표시하고 싶습니다.그러므로 나는 그 내용이 필요합니다.wp_nav_menu
REST API에서 노출됩니다.
API를 통해 메뉴 구조에 액세스하는 표준 방법이 있습니까?그렇지 않은 경우 이 기능을 다루는 최신 플러그인이 있습니까?구글은 시대에 뒤떨어진 해결책만 꺼냈습니다.
baymax의 제안에 영감을 받아 다음 두 가지 기능을 만들었습니다. 그래서 가능한 모든 메뉴를 가져오는 것과 개별 메뉴에 대한 정보를 검색하는 것이 모두 가능합니다.
REST API에 메뉴를 추가하고 개별 메뉴 항목을 검색하는 데 사용할 수 있는 두 가지 기능이 있습니다.
이를 통해 다음 두 가지 모두에 액세스할 수 있습니다.
https://your-wp-domain-url.com/wp-json/custom/menu 및
https://your-wp-domain-url.com/wp-json/custom/menu/ {menuID}
// Menu locations
add_action( 'rest_api_init', function() {
register_rest_route( 'custom', '/menu/', array(
'methods' => 'GET',
'callback' => 'wp_menu_route',
));
});
function wp_menu_route() {
$menuLocations = get_nav_menu_locations(); // Get nav locations set in theme, usually functions.php)
return $menuLocations;
}
// Individual menus
add_action( 'rest_api_init', function() {
register_rest_route( 'custom', '/menu/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'wp_menu_single',
));
});
function wp_menu_single($data) {
$menuID = $data['id']; // Get the menu from the ID
$primaryNav = wp_get_nav_menu_items($menuID); // Get the array of wp objects, the nav items for our queried location.
return $primaryNav;
}
이거 어때요?이 경로를 사용하여 "기본" 메뉴에 액세스할 수 있습니다. -> https://your-wp-domain-url.com/wp-json/custom-name/menu
register_nav_menus()의 Primary 메뉴를 사용해야 합니다.나의 WP Theme 함수에서.php 나는 이 대사들을 가지고 있습니다.('module-1'이 제 기본 메뉴입니다.)
register_nav_menus( array(
'menu-1' => esc_html__( 'Primary', 'text-domain' ),
));
이 줄에 이 '메뉴-1'을 추가합니다.
===> $209ID = $menuLocations['menu-1'];
//https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/
function wp_menu_route() {
// register_nav_menus( array(
// 'menu-1' => esc_html__( 'Primary', 'text-domain' ),
// ) );
// using register_nav_menus primary menu name -> 'menu-1'
$menuLocations = get_nav_menu_locations(); // Get nav locations set in theme, usually functions.php)
// returns an array of menu locations ([LOCATION_NAME] = MENU_ID);
$menuID = $menuLocations['menu-1']; // Get the *primary* menu added in register_nav_menus()
$primaryNav = wp_get_nav_menu_items($menuID); // Get the array of wp objects, the nav items for our queried location.
return $primaryNav;
}
add_action( 'rest_api_init', function () {
//https://your-wp-domain-url.com/wp-json/custom-name/menu
register_rest_route( 'custom-name', '/menu', array(
'methods' => 'GET',
'callback' => 'wp_menu_route',
) );
} );
첫 번째 단계: WP-REST-API V2 메뉴 설치 https://wordpress.org/plugins/wp-rest-api-v2-menus/
두 번째 단계:등록된 모든 메뉴의 도메인 이름/wp-json/메뉴/v1/메뉴 목록.특정 메뉴의 도메인 이름/wp-json/menus/v1/menus/slug 데이터.
언급URL : https://stackoverflow.com/questions/51763749/how-to-access-wordpress-menu-item-through-the-rest-api
'programing' 카테고리의 다른 글
Oracle CLOB가 4000자를 초과하여 삽입할 수 없습니까? (0) | 2023.07.06 |
---|---|
2D 배열을 매개 변수로 전달할 때 열 크기를 지정해야 하는 이유는 무엇입니까? (0) | 2023.07.06 |
는 "HttpRequest"에 대한 상수입니다.RequestType" 및 "WebRequest"를 선택합니다..NET의 Method" 값? (0) | 2023.07.06 |
TypeScript | Array.from | 오류 TS2339: 'from' 속성이 'ArrayConstructor' 유형에 없습니다. (0) | 2023.07.06 |
루비의 여러 줄 설명? (0) | 2023.07.06 |