add endpoint authors
This commit is contained in:
@@ -1,56 +1,77 @@
|
||||
---
|
||||
// pages/author/[slug]/index.astro
|
||||
import { wpClient } from '@lib/wp-client';
|
||||
import MainLayout from '@layouts/MainLayout.astro';
|
||||
import { getAuthorData, getPostsByCoauthorLogin } from '@lib/api/authors';
|
||||
|
||||
import ContentGrid from '@components/ContentGrid.astro';
|
||||
|
||||
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 author = await getAuthorData(slug);
|
||||
|
||||
const authorData = await getAuthorData(slug);
|
||||
const data = await getPostsByCoauthorLogin(slug);
|
||||
const posts = data.posts;
|
||||
|
||||
// Если автор не найден - 404
|
||||
//if (!authorData) {
|
||||
// return Astro.redirect('/404');
|
||||
//}
|
||||
|
||||
const { author, posts } = authorData;
|
||||
---
|
||||
|
||||
<h1>Author: {author.name || slug}</h1>
|
||||
<MainLayout
|
||||
title={`Публикации автора: ${slug}`}
|
||||
description={`Информационное агентство Деловой журнал Профиль - записи по тегу ${slug}`}
|
||||
>
|
||||
|
||||
{author && (
|
||||
<div class="author-card">
|
||||
{author.avatar && (
|
||||
<img
|
||||
src={author.avatar}
|
||||
alt={author.name}
|
||||
width="192"
|
||||
height="192"
|
||||
class="avatar"
|
||||
/>
|
||||
)}
|
||||
|
||||
<h1>{author.name}</h1>
|
||||
|
||||
{(author.firstName || author.lastName) && (
|
||||
<p class="full-name">
|
||||
{author.firstName} {author.lastName}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{author.bio && (
|
||||
<div class="bio">{author.bio}</div>
|
||||
)}
|
||||
|
||||
{author.social && Object.values(author.social).some(Boolean) && (
|
||||
<div class="social-links">
|
||||
{author.social.twitter && (
|
||||
<a href={author.social.twitter}>Twitter</a>
|
||||
)}
|
||||
{author.social.facebook && (
|
||||
<a href={author.social.facebook}>Facebook</a>
|
||||
)}
|
||||
{author.social.instagram && (
|
||||
<a href={author.social.instagram}>Instagram</a>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<ContentGrid
|
||||
items={posts}
|
||||
pageInfo={data.pageInfo}
|
||||
slug={slug}
|
||||
showCount={false}
|
||||
type='author'
|
||||
perLoad={11}
|
||||
/>
|
||||
</MainLayout>
|
||||
|
||||
|
||||
{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