Intereting Posts
WP_Customize_Color_Control исключает символ # Автоматически назначать настраиваемую запись для пользовательской таксономии на основе настраиваемого значения поля Не загружать тему для страницы FROM plugin EDITED Как создать короткий код для настраиваемого шаблона страницы Is_template, как добавить еще один шаблон Автоматическое добавление новых тегов в подменю Контролируйте, какие пользовательские сообщения пользователь может видеть Стиль WP UI в коротком кодексе – любые предложения? Добавить фильтр таксомии в запрос одиночной почты? Зарегистрируйте нового пользователя и добавьте значение строки данных для него Как отображать несколько изображений в настраиваемом поле Создать форму aa для пользовательских терминов таксономии jQuery AJAX добавить json2 зависимость? Уменьшите количество SQL-запросов внутри цикла WP_Query для извлечения данных автора Как изменить строки в теме / ядре?

wp json api – получить сообщения по категориям

Я добавил метод к основному контроллеру плагина WP JSON API, который извлекает сообщения по категориям, но, тем не менее, получает только одну категорию данных. Пожалуйста, найдите код ниже.

public function module_data() { global $json_api; $defaults = array( 'ignore_sticky_posts' => true, 'category_name' => 'food'); $posts = $json_api->introspector->get_posts($defaults); $result = $this->posts_result($posts); unset($result['pages'], $result['count_total']); foreach ($result['posts'] as $key) { $key->thumbnail_image = $key->thumbnail_images['thumbnail']->url; $key->large_image = $key->thumbnail_images['large']->url; unset($key->id, $key->type, $key->slug, $key->url, $key->status, $key->title_plain, $key->excerpt, $key->categories, $key->tags, $key->author, $key->comments, $key->custom_fields, $key->comment_status, $key->comment_count, $key->attachments, $key->date, $key->modified, $key->thumbnail, $key->thumbnail_size , $key->thumbnail_images['full'], $key->thumbnail_images['medium'], $key->thumbnail_images['medium_large'], $key->thumbnail_images); } $res['food'] = array_reverse($result['posts']); $defaults_two = array( 'ignore_sticky_posts' => true, 'category_name' => 'history'); $posts_two = $json_api->introspector->get_posts($defaults_two); $result_two = $this->posts_result($posts_two); unset($result_two['pages'], $result_two['count_total']); foreach ($result_two['posts'] as $key) { $key->thumbnail_image = $key->thumbnail_images['thumbnail']->url; $key->large_image = $key->thumbnail_images['large']->url; unset($key->id, $key->type, $key->slug, $key->url, $key->status, $key->title_plain, $key->excerpt, $key->categories, $key->tags, $key->author, $key->comments, $key->custom_fields, $key->comment_status, $key->comment_count, $key->attachments, $key->date, $key->modified, $key->thumbnail, $key->thumbnail_size , $key->thumbnail_images['full'], $key->thumbnail_images['medium'], $key->thumbnail_images['medium_large'], $key->thumbnail_images); } $res['history'] = array_reverse($result_two['posts']); return $res; } 

Вывод:

 { "status": "ok", "food": [ { "title": "Hyderabad Cuisine", "content": "<p>Hyderabadi cuisine is a delightful melting pot of Nizam, Turkish, Arabic and Andhra culinary delights. The delicate taste of long, fragrant rice, the lovely aroma of fresh spices, layers of delicious meat and a tinge of saffron come together to create the delectable Hyderabadi Biryani. But, authentic Hyderabadi cuisine goes beyond the much-loved Hyderabadi Biryani and would take your taste buds on a tantalizing journey of flavors offered through age-old recipes and special ingredients from the royal kitchen of Nizams.</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_15@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_15@2x.png" }, { "title": "Adaa, Taj Falaknuma Palace", "content": "<p>An opulent symbol of the Nizam's glory, Adaa is a food connoisseur's heaven with gastronomic delights served the royal way. The restaurant is decorated with beautifully carved mirrors, life-size portraits of the Nizam ruler, candelabras and sparkling chandeliers. Lunch is an elaborate affair with dishes based on secret recipes from the Nizam's era. The chef and his team give an impressive brief about the various courses of the meal and present each dish with elan.</p>\n<p>From succulent kebabs coated in exotic herbs to soft Indian breads, each bite is a delight on its own celebrating the earthy, vibrant and wholesome flavors of Hyderabad. Some popular favorites from this fine dining restaurant are, Rogan Josh (an aromatic lamb dish), Hyderabadi Kacchi Biryani (raw meat and rice are cooked together creating a perfect blend of flavor), Nizam Handi (vegetables or meat cooked in Nizam style), Patthar Ka Gosht (meat marinated overnight and cooked on stone), Dahi Ke Kebab (tangy yoghurt patties that almost melt in the mouth), and Malai Kulfi Faluda (Indian ice cream with vermicelli).</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_16@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_16@2x.png" }, { "title": "Paradise", "content": "<p>With the influence of ancient food delicacies spread across Hyderabad, Paradise restaurant has become an integral part of the Hyderabadi food culture. Dig into the spicy food made from the aromatic ingredients found in the royal kitchen of Mughals. This outlet provides a fine dine experience to its customers with dishes that resonate the hearty, real and wholesome flavors of royal cuisine.</p>\n<p>The most famous delicacies from this eatery which whips up amazing dishes include the authentic Hyderabadi Chicken / Mutton Biryani (a delicious combination of rice, spices and meat) and Apollo Fish (spiced batter coated fish) &amp; so many more.</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_17@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_17@2x.png" }, { "title": "Khan Saab", "content": "<p>The restaurant is famous for its delicacies having a very distinguishable taste and aroma. The eatery has dishes with perfect amalgamation of typical Mughlai flavors with a blend of rich spices. This outlet is famous for conjure delicacies that are rich in taste and texture with exquisite aromas.</p>\n<p>With an elegant and opulent Peshawari ambience, this restaurant has authentic Hyderabadi food. The eatery offers a range of rare and popular dishes that are made out of exotic spices. Some of the delicacies available from this famous restaurant are Chicken Biryani (chicken cooked with rice and spices), Taar Qorma (royal Indian goat curry), Ghuti Dal ka Shorba (a type of dal / lentil), Paan Halwa (sweet) &amp; many more.</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_18@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_18@2x.png" } ], "history": [ { "title": "Hyderabad Cuisine", "content": "<p>Hyderabadi cuisine is a delightful melting pot of Nizam, Turkish, Arabic and Andhra culinary delights. The delicate taste of long, fragrant rice, the lovely aroma of fresh spices, layers of delicious meat and a tinge of saffron come together to create the delectable Hyderabadi Biryani. But, authentic Hyderabadi cuisine goes beyond the much-loved Hyderabadi Biryani and would take your taste buds on a tantalizing journey of flavors offered through age-old recipes and special ingredients from the royal kitchen of Nizams.</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_15@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_15@2x.png" }, { "title": "Adaa, Taj Falaknuma Palace", "content": "<p>An opulent symbol of the Nizam's glory, Adaa is a food connoisseur's heaven with gastronomic delights served the royal way. The restaurant is decorated with beautifully carved mirrors, life-size portraits of the Nizam ruler, candelabras and sparkling chandeliers. Lunch is an elaborate affair with dishes based on secret recipes from the Nizam's era. The chef and his team give an impressive brief about the various courses of the meal and present each dish with elan.</p>\n<p>From succulent kebabs coated in exotic herbs to soft Indian breads, each bite is a delight on its own celebrating the earthy, vibrant and wholesome flavors of Hyderabad. Some popular favorites from this fine dining restaurant are, Rogan Josh (an aromatic lamb dish), Hyderabadi Kacchi Biryani (raw meat and rice are cooked together creating a perfect blend of flavor), Nizam Handi (vegetables or meat cooked in Nizam style), Patthar Ka Gosht (meat marinated overnight and cooked on stone), Dahi Ke Kebab (tangy yoghurt patties that almost melt in the mouth), and Malai Kulfi Faluda (Indian ice cream with vermicelli).</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_16@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_16@2x.png" }, { "title": "Paradise", "content": "<p>With the influence of ancient food delicacies spread across Hyderabad, Paradise restaurant has become an integral part of the Hyderabadi food culture. Dig into the spicy food made from the aromatic ingredients found in the royal kitchen of Mughals. This outlet provides a fine dine experience to its customers with dishes that resonate the hearty, real and wholesome flavors of royal cuisine.</p>\n<p>The most famous delicacies from this eatery which whips up amazing dishes include the authentic Hyderabadi Chicken / Mutton Biryani (a delicious combination of rice, spices and meat) and Apollo Fish (spiced batter coated fish) &amp; so many more.</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_17@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_17@2x.png" }, { "title": "Khan Saab", "content": "<p>The restaurant is famous for its delicacies having a very distinguishable taste and aroma. The eatery has dishes with perfect amalgamation of typical Mughlai flavors with a blend of rich spices. This outlet is famous for conjure delicacies that are rich in taste and texture with exquisite aromas.</p>\n<p>With an elegant and opulent Peshawari ambience, this restaurant has authentic Hyderabadi food. The eatery offers a range of rare and popular dishes that are made out of exotic spices. Some of the delicacies available from this famous restaurant are Chicken Biryani (chicken cooked with rice and spices), Taar Qorma (royal Indian goat curry), Ghuti Dal ka Shorba (a type of dal / lentil), Paan Halwa (sweet) &amp; many more.</p>\n", "thumbnail_image": "http://localhost/wp/wp-content/uploads/2016/03/img_18@2x-150x150.png", "large_image": "http://localhost/wp/wp-content/uploads/2016/03/img_18@2x.png" } ] } 

Пожалуйста, помогите, любая помощь будет высоко оценена. Заранее спасибо.

Я решил проблему по-другому. Сначала я извлек все записи, включив в них название категории. Проверьте ответ ниже.

 public function module_data() { global $json_api; $url = parse_url($_SERVER['REQUEST_URI']); $defaults = array( 'ignore_sticky_posts' => true, 'posts_per_page' => -1, 'order_by' => 'date', 'order' => 'ASC' ); $query = wp_parse_args($url['query']); unset($query['json']); unset($query['post_status']); $query = array_merge($defaults, $query); $posts = $json_api->introspector->get_posts($query); $result = $this->posts_result($posts); unset($result['pages'], $result['count_total']); foreach ($result['posts'] as $key) { $key->thumbnail_image = $key->thumbnail_images['thumbnail']->url; $key->large_image = $key->thumbnail_images['large']->url; $key->category = $key->categories[0]->title; //$key->categories unset($key->id, $key->type, $key->slug, $key->url, $key->status, $key->title_plain, $key->excerpt, $key->tags, $key->author, $key->comments, $key->custom_fields, $key->comment_status, $key->comment_count, $key->categories, $key->attachments, $key->date, $key->modified, $key->thumbnail, $key->thumbnail_size , $key->thumbnail_images['full'], $key->thumbnail_images['medium'], $key->thumbnail_images['medium_large'], $key->thumbnail_images); } $result['posts'] = $result['posts']; return $result; } с public function module_data() { global $json_api; $url = parse_url($_SERVER['REQUEST_URI']); $defaults = array( 'ignore_sticky_posts' => true, 'posts_per_page' => -1, 'order_by' => 'date', 'order' => 'ASC' ); $query = wp_parse_args($url['query']); unset($query['json']); unset($query['post_status']); $query = array_merge($defaults, $query); $posts = $json_api->introspector->get_posts($query); $result = $this->posts_result($posts); unset($result['pages'], $result['count_total']); foreach ($result['posts'] as $key) { $key->thumbnail_image = $key->thumbnail_images['thumbnail']->url; $key->large_image = $key->thumbnail_images['large']->url; $key->category = $key->categories[0]->title; //$key->categories unset($key->id, $key->type, $key->slug, $key->url, $key->status, $key->title_plain, $key->excerpt, $key->tags, $key->author, $key->comments, $key->custom_fields, $key->comment_status, $key->comment_count, $key->categories, $key->attachments, $key->date, $key->modified, $key->thumbnail, $key->thumbnail_size , $key->thumbnail_images['full'], $key->thumbnail_images['medium'], $key->thumbnail_images['medium_large'], $key->thumbnail_images); } $result['posts'] = $result['posts']; return $result; } с public function module_data() { global $json_api; $url = parse_url($_SERVER['REQUEST_URI']); $defaults = array( 'ignore_sticky_posts' => true, 'posts_per_page' => -1, 'order_by' => 'date', 'order' => 'ASC' ); $query = wp_parse_args($url['query']); unset($query['json']); unset($query['post_status']); $query = array_merge($defaults, $query); $posts = $json_api->introspector->get_posts($query); $result = $this->posts_result($posts); unset($result['pages'], $result['count_total']); foreach ($result['posts'] as $key) { $key->thumbnail_image = $key->thumbnail_images['thumbnail']->url; $key->large_image = $key->thumbnail_images['large']->url; $key->category = $key->categories[0]->title; //$key->categories unset($key->id, $key->type, $key->slug, $key->url, $key->status, $key->title_plain, $key->excerpt, $key->tags, $key->author, $key->comments, $key->custom_fields, $key->comment_status, $key->comment_count, $key->categories, $key->attachments, $key->date, $key->modified, $key->thumbnail, $key->thumbnail_size , $key->thumbnail_images['full'], $key->thumbnail_images['medium'], $key->thumbnail_images['medium_large'], $key->thumbnail_images); } $result['posts'] = $result['posts']; return $result; } и public function module_data() { global $json_api; $url = parse_url($_SERVER['REQUEST_URI']); $defaults = array( 'ignore_sticky_posts' => true, 'posts_per_page' => -1, 'order_by' => 'date', 'order' => 'ASC' ); $query = wp_parse_args($url['query']); unset($query['json']); unset($query['post_status']); $query = array_merge($defaults, $query); $posts = $json_api->introspector->get_posts($query); $result = $this->posts_result($posts); unset($result['pages'], $result['count_total']); foreach ($result['posts'] as $key) { $key->thumbnail_image = $key->thumbnail_images['thumbnail']->url; $key->large_image = $key->thumbnail_images['large']->url; $key->category = $key->categories[0]->title; //$key->categories unset($key->id, $key->type, $key->slug, $key->url, $key->status, $key->title_plain, $key->excerpt, $key->tags, $key->author, $key->comments, $key->custom_fields, $key->comment_status, $key->comment_count, $key->categories, $key->attachments, $key->date, $key->modified, $key->thumbnail, $key->thumbnail_size , $key->thumbnail_images['full'], $key->thumbnail_images['medium'], $key->thumbnail_images['medium_large'], $key->thumbnail_images); } $result['posts'] = $result['posts']; return $result; }