From c09a941d3fca18a6bf8669ba9360ff6a9b0d7c72 Mon Sep 17 00:00:00 2001 From: Kr1PtunZ Date: Thu, 30 May 2024 15:13:41 +0500 Subject: [PATCH] =?UTF-8?q?API=20=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=BE?= =?UTF-8?q?=D0=B4=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BF=D0=BE=D1=81=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=20permalink=20=D0=B8=20=D0=BF=D0=BE=D1=81=D0=BB?= =?UTF-8?q?=D0=B5=D0=B4=D1=83=D1=8E=D1=89=D0=B8=D1=85=20=D0=BF=D0=BE=20?= =?UTF-8?q?=D0=BA=D0=B0=D1=82=D0=B5=D0=BE=D1=80=D0=B8=D1=8F=D0=BC.=20Readm?= =?UTF-8?q?e.txt=20=D0=BF=D0=BE=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D1=80=D0=B0=D0=BC=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-readmi.txt | 9 +++++ .../simple-theme/api-posts/api-posts.php | 34 ++++++------------- 2 files changed, 19 insertions(+), 24 deletions(-) create mode 100644 api-readmi.txt diff --git a/api-readmi.txt b/api-readmi.txt new file mode 100644 index 0000000..4e4d1d7 --- /dev/null +++ b/api-readmi.txt @@ -0,0 +1,9 @@ +API + +API принимает запросы по URL /wp-json/api-posts/v1/endlessPosts + +Через GET-запрос принимает значение permalink, выводит информацию по посту чей permalink пришёл, далее 10 пермалинков и +ID постов из совпадающих категорий. + +Пример: +/wp-json/api-posts/v1/endlessPosts?permalink=http://pamtest.ru/641-kak-zarabatyvat-bolshe-na-osago-i-ipotechnom-strahovanii/ \ No newline at end of file diff --git a/wp-content/themes/simple-theme/api-posts/api-posts.php b/wp-content/themes/simple-theme/api-posts/api-posts.php index 8896590..a82fb17 100644 --- a/wp-content/themes/simple-theme/api-posts/api-posts.php +++ b/wp-content/themes/simple-theme/api-posts/api-posts.php @@ -10,45 +10,36 @@ function endlessPosts() )); } -function getEndlessPostsResults($data, $paged = 1) +function getEndlessPostsResults($data) { global $wpdb; - $link = $data['permalink']; + $result = []; $tag_slug = url_to_postid($link); //Получаем айди поста по пермалинку -// var_dump($tag_slug); $tags = get_the_tags($tag_slug); //Получаем тэги по айди -// var_dump($tags[0]->term_id); -// die(); $i = 0; $postSlugs = []; foreach ($tags as $key => $slug){ //Создаём массив с ID тэгов $postSlugs[] += $tags[$i]->term_id; -// if ($key < count($tags) - 1) { -// $postSlugs .= ', '; -// } $i++; } - -// $tag_name = $tag->name; - - - $id_from_permalink = explode('/', $link); $id_from_permalink = explode('-', $id_from_permalink[3]); $id = $id_from_permalink[0]; - $post_id = $id; -// $category = get_the_tags($post_id); - $result = array(); + + $requestedPost = get_post($id); + + $result[] = ['requested-post' => $requestedPost]; + $args = array( // Создаем массив с параметрами запроса 'tag__and' => $postSlugs, 'posts_per_page' => 10, - 'paged' => $paged, - 'orderby'=>'date' + 'orderby'=>'date', + 'order'=> 'DESC' ); $posts = new WP_Query($args); // Инициализация запроса @@ -70,13 +61,8 @@ function getEndlessPostsResults($data, $paged = 1) $actualTag = get_the_tags(); $terms = get_the_category(); $result[] = array( + 'post_id' => get_the_ID(), 'permalink' => get_the_permalink(), - 'terms' => $terms[0]->slug, - 'title' => get_the_title(), - 'author' => get_the_author(), - 'date' => get_the_date(), - 'post-body' =>get_the_content(), - 'tags' => get_the_tags()[0]->name ); }