add tags logic
This commit is contained in:
56
src/pages/author/[slug]/index.astro
Normal file
56
src/pages/author/[slug]/index.astro
Normal file
@@ -0,0 +1,56 @@
|
||||
---
|
||||
// pages/author/[slug]/index.astro
|
||||
import { wpClient } from '@lib/wp-client';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
const { slug } = Astro.params;
|
||||
|
||||
// Функция для получения данных автора
|
||||
async function getAuthorData(authorSlug) {
|
||||
try {
|
||||
// Используем ваш кастомный эндпоинт или стандартный WP endpoint
|
||||
const data = await wpClient.get(`my/v1/author-posts/${authorSlug}/1`);
|
||||
|
||||
if (!data) {
|
||||
// Если кастомный эндпоинт не работает, пробуем стандартный
|
||||
const users = await wpClient.get('wp/v2/users', { slug: authorSlug });
|
||||
if (users && users.length > 0) {
|
||||
return { author: users[0], posts: [] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching author:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const authorData = await getAuthorData(slug);
|
||||
|
||||
// Если автор не найден - 404
|
||||
//if (!authorData) {
|
||||
// return Astro.redirect('/404');
|
||||
//}
|
||||
|
||||
const { author, posts } = authorData;
|
||||
---
|
||||
|
||||
<h1>Author: {author.name || slug}</h1>
|
||||
|
||||
{posts && posts.length > 0 ? (
|
||||
<div>
|
||||
<h2>Статьи автора:</h2>
|
||||
<ul>
|
||||
{posts.map(post => (
|
||||
<li key={post.id}>
|
||||
<a href={post.link}>{post.title}</a>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
) : (
|
||||
<p>У автора пока нет статей</p>
|
||||
)}
|
||||
Reference in New Issue
Block a user