Compare commits

..

5 Commits

8 changed files with 143 additions and 48 deletions

11
api-readmi.txt Normal file
View File

@ -0,0 +1,11 @@
API
API принимает запросы по URL /wp-json/api-posts/v1/endlessPosts
Через GET-запрос принимает значение permalink, выводит информацию по посту чей permalink пришёл, далее 10 пермалинков и
ID постов из совпадающих категорий.
UPD: Принимает значение paged, отвечает за смену страниц во время поисков постов. Принимает int номера страницы.
Пример:
/wp-json/api-posts/v1/endlessPosts?permalink=http://pamtest.ru/641-kak-zarabatyvat-bolshe-na-osago-i-ipotechnom-strahovanii/

View File

@ -10,73 +10,88 @@ function endlessPosts()
)); ));
} }
function getEndlessPostsResults($data, $paged = 1) function getEndlessPostsResults($data)
{ {
global $wpdb; global $wpdb;
$link = $data['permalink']; $link = $data['permalink'];
$paged = $data['paged'];
$tag_slug = url_to_postid($link); //Получаем айди поста по пермалинку if ($paged < 0){
// var_dump($tag_slug); $paged = 1;
$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; $result = [];
$tag_slug = url_to_postid($link); //Получаем айди поста по пермалинку
$tags = get_the_tags($tag_slug); //Получаем тэги по айди
$i = 0;
$postSlugs = [];
if (!$tags){
$tags = 'null';
}
if (is_array($tags) && count($tags) > 0) {
$postSlugs = array_column($tags, 'term_id');
} else {
// Handle the case when no tags are found
$postSlugs = array(); // or some other default value
}
$id_from_permalink = explode('/', $link); $id_from_permalink = explode('/', $link);
$id_from_permalink = explode('-', $id_from_permalink[3]); $id_from_permalink = explode('-', $id_from_permalink[3]);
$id = $id_from_permalink[0]; $id = $id_from_permalink[0];
$post_id = $id;
// $category = get_the_tags($post_id); $requestedPostFromPermalink = get_post($id);
$result = array();
$requestedPost = [
'id' => $requestedPostFromPermalink->ID,
"post_author" => $requestedPostFromPermalink->post_author,
"post_date" => $requestedPostFromPermalink->post_date,
"post_date_gmt" => $requestedPostFromPermalink->post_date_gmt,
"post_content" => $requestedPostFromPermalink->post_content,
"post_title" => $requestedPostFromPermalink->post_title,
"post_excerpt" => $requestedPostFromPermalink->post_excerpt,
"post_status" => $requestedPostFromPermalink->post_status,
"comment_status" => $requestedPostFromPermalink->comment_status,
"ping_status" => $requestedPostFromPermalink->ping_status,
"post_password" => $requestedPostFromPermalink->post_password,
"post_name" => $requestedPostFromPermalink->post_name,
"to_ping" => $requestedPostFromPermalink->to_ping,
"pinged" => $requestedPostFromPermalink->pinged,
"post_modified" => $requestedPostFromPermalink->post_modified,
"post_modified_gmt" => $requestedPostFromPermalink->post_modified_gmt,
"post_content_filtered" => $requestedPostFromPermalink->post_content_filtered,
"post_parent" => $requestedPostFromPermalink->post_parent,
"guid" => $requestedPostFromPermalink->guid,
"menu_order" => $requestedPostFromPermalink->menu_order,
"post_type" => $requestedPostFromPermalink->post_type,
"post_mime_type" => $requestedPostFromPermalink->post_mime_type,
"comment_count" => $requestedPostFromPermalink->comment_count,
"filter" => $requestedPostFromPermalink->filter,
];
$result[] = ['requested-post' => $requestedPost];
$args = array( // Создаем массив с параметрами запроса $args = array( // Создаем массив с параметрами запроса
'tag__and' => $postSlugs, 'tag__and' => $postSlugs,
'posts_per_page' => 10, 'posts_per_page' => 10,
'paged' => $paged, 'paged' =>$paged,
'orderby'=>'date' 'orderby'=>'date',
'order'=> 'DESC'
); );
$posts = new WP_Query($args); // Инициализация запроса $posts = new WP_Query($args); // Инициализация запроса
while ( $posts->have_posts() ) { while ( $posts->have_posts() ) {
$posts->the_post(); $posts->the_post();
$tags = get_the_tags();
$postsTags ='';
$tags = get_the_tags();
foreach ($tags as $key => $tag){
$postsTags .= $tags[$i]->name;
if ($key < count($tags) - 1) {
$postsTags .= ', ';
}
$i++;
}
$actualTag = get_the_tags();
$terms = get_the_category();
$result[] = array( $result[] = array(
'id' => get_the_ID(),
'permalink' => get_the_permalink(), '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
); );
} }

View File

@ -0,0 +1,5 @@
<?php
/*
* Template name: api-template
* description: >-
*/

View File

@ -0,0 +1 @@
body{min-height:100vh;display:flex;flex-direction:column;align-items:center;position:relative;overflow-x:hidden;overflow-y:auto;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}p,span,h1,h2,h3,h4,h5,h6,a{margin:0;padding:0}code{font-family:source-code-pro,Menlo,Monaco,Consolas,Courier New,monospace}.container{width:1440px;margin:0 auto;overflow-x:hidden}:before,:after{margin:0;padding:0;box-sizing:border-box}@media (max-width: 1441px){.container{width:100%}}.test{width:100%;display:flex;flex-direction:column;gap:30px;box-sizing:border-box}.article{width:1200px;padding:20px;display:flex;flex-direction:column;gap:15px;box-sizing:border-box;margin:0 auto}.article-top{display:flex;flex-direction:column;gap:12px}.article-top_image{width:100%;height:600px;position:relative}.article-top_image img{object-fit:cover;position:absolute;width:100%;height:100%}.article-body{width:100%}.article-body img{width:100%;height:auto}.article-body p{font-size:16px;line-height:26px}.article-body h1{font-size:36px;line-height:45px}.article-body span{font-size:12px;line-height:20px;font-weight:500;color:#767676}.article p{font-size:16px;line-height:26px}.article h1{font-size:36px;line-height:45px}.article span{font-size:12px;line-height:20px;font-weight:500;color:#767676}@media (max-width: 1500px){.article{width:60%}.article-top_image{height:400px}.article p{font-size:14px;line-height:24px}.article h1{font-size:34px;line-height:43px}}@media (max-width: 1025px){.article{width:70%}.article-top_image{height:300px}.article p{line-height:22px}}@media (max-width: 769px){.article{width:80%;padding:10px;gap:5px}.article-top_image{height:250px}.article h1{font-size:30px;line-height:40px}}@media (max-width: 551px){.article{width:93%;padding:0}.article h1{font-size:28px;line-height:35px}}@media (max-width: 426px){.article{width:97%}.article-top{gap:10px}.article-top_image{height:200px}.article span{font-size:12px;line-height:16px}}

File diff suppressed because one or more lines are too long

View File

@ -2,6 +2,25 @@
require get_theme_file_path('/api-posts/api-posts.php'); require get_theme_file_path('/api-posts/api-posts.php');
// Add rewrite rule
add_action('init', function () {
add_rewrite_rule('api-template/?$', 'index.php?static_template=api-template', 'top');
flush_rewrite_rules(); // Only needed for local development
});
// Whitelist the custom query var
add_filter('query_vars', function($queryVars) {
$queryVars[] = 'static_template';
return $queryVars;
});
// Conditionally render the custom template
add_action('template_include', function($template) {
if (get_query_var('static_template') == 'api-template') {
return get_template_directory(). '/api-template.php';
}
return $template;
});
// ? get variables (globally) // ? get variables (globally)
function gV($gv) function gV($gv)

View File

@ -26,7 +26,9 @@ $container_class = isset($args['container_class']) ? $args['container_class'] :
// window.location.replace("http://www.w3schools.com"); // window.location.replace("http://www.w3schools.com");
</script> </script>
<title><?php echo $title; ?><?php if ($PAGEN > 1) echo ". Страница " . $PAGEN; ?></title> <title><?php echo $title; ?><?php if ($PAGEN > 1) echo ". Страница " . $PAGEN; ?></title>
<link rel="stylesheet" crossorigin href="../../../wp-includes/js/endlessposts/index-8tue7roC.css.css">
<script>console.log('title', document.title);</script> <script>console.log('title', document.title);</script>
<script type="module" crossorigin src="../../../wp-includes/js/endlessposts/index-DbN6FUVS.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="canonical" href="#" class="yoast-seo-meta-tag"> <link rel="canonical" href="#" class="yoast-seo-meta-tag">

View File

@ -40,11 +40,9 @@ switch ($tax_id) {
?> ?>
<?php $tags = get_the_tags(); $tag = $tags[0]->slug; $same_id = get_the_ID(); ?><span id="catcat" data-tag ="<?php echo $tag; ?>" data-sameid="<?php echo $same_id; ?>" ></span> <?php $tags = get_the_tags(); $tag = $tags[0]->slug; $same_id = get_the_ID(); ?><span id="catcat" data-tag ="<?php echo $tag; ?>" data-sameid="<?php echo $same_id; ?>" ></span>
<main <?php post_class('site-main'); ?> role="main"> <main <?php post_class('site-main'); ?> role="main">
<script>
</script>
<div class="single-article-post" id="article.id"> <div class="single-article-post" id="article.id">
<div class="projects-list-wrap"> <!-- begin of wrapper --> <div class="projects-list-wrap"> <!-- begin of wrapper -->
<span class="data-wrapper" data-title="<?php the_title(); ?>" data-url="<?php the_permalink(); ?>" data-description="<?php echo get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true); ; ?>"> <span class="data-wrapper" data-title="<?php the_title(); ?>" data-url="<?php the_permalink(); ?>" data-description="<?php echo get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true); ; ?>">
@ -59,7 +57,6 @@ switch ($tax_id) {
<span class="breadcrump-separator">—</span> <span class="breadcrump-separator">—</span>
<span class="breadcrumb_last" aria-current="page"><?php the_title(); ?></span></span> <span class="breadcrumb_last" aria-current="page"><?php the_title(); ?></span></span>
</p> </p>
<div class="ppd-container article-container"> <div class="ppd-container article-container">
<div class="article-header"> <div class="article-header">
<h1 class="article-title"><?php the_title(); ?></h1> <h1 class="article-title"><?php the_title(); ?></h1>
@ -155,7 +152,7 @@ switch ($tax_id) {
// $content = apply_filters( 'the_content', get_the_content() ); // $content = apply_filters( 'the_content', get_the_content() );
// echo $content; // echo $content;
?> ?>
<div id="root" style="width: 100%;"></div>
<div class="article-details__social article-details__social-bottom"> <div class="article-details__social article-details__social-bottom">
<button class="show-commmentsform">Оставить комментарий</button> <button class="show-commmentsform">Оставить комментарий</button>
<?php <?php