@charset "UTF-8";
/*
    Template: swell
    Theme Name: SWELL CHILD
    Theme URI: https://swell-theme.com/
    Description: SWELLの子テーマ
    Version: 1.0.0
    Author: LOOS WEB STUDIO
    Author URI: https://loos-web-studio.com/

    License: GNU General Public License
    License URI: http://www.gnu.org/licenses/gpl.html
*/

function add_custom_json_ld() {
    // 1. トップページ または 固定ページ の場合（組織情報）
    if ( is_front_page() || is_page() ) {
        $json_ld = [
            "@context" => "http://schema.org",
            "@type" => "MedicalOrganization",
            "name" => "伝統鍼灸一滴庵",
            "address" => [
                "@type" => "PostalAddress",
                "streetAddress" => "南森町2丁目1-23 藤原ビル203",
                "addressLocality" => "大阪市北区",
                "addressRegion" => "大阪府",
                "postalCode" => "5300054",
                "addressCountry" => "JP"
            ],
            "geo" => [
                "@type" => "GeoCoordinates",
                "latitude" => "34.698237",
                "longitude" => "135.51074"
            ],
            "telephone" => "+81-668092486",
            "openingHoursSpecification" => [
                [
                    "@type" => "OpeningHoursSpecification",
                    "dayOfWeek" => ["Monday", "Tuesday", "Wednesday", "Friday"],
                    "opens" => "9:30",
                    "closes" => "20:00"
                ],
                [
                    "@type" => "OpeningHoursSpecification",
                    "dayOfWeek" => ["Thursday"],
                    "opens" => "9:30",
                    "closes" => "11:00"
                ],
                [
                    "@type" => "OpeningHoursSpecification",
                    "dayOfWeek" => ["Saturday"],
                    "opens" => "9:30",
                    "closes" => "14:00"
                ]
            ],
            "image" => "https://ittekian.com/wp-content/uploads/2022/12/cropped-icon-512x512-1.png",
            "url" => "https://ittekian.com"
        ];
        
        echo '<script type="application/ld+json">' . json_encode($json_ld, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . '</script>' . "\n";
    }
    
    // 2. 投稿ページ（ブログ詳細）の場合
    elseif ( is_single() ) {
        global $post;
        $post_id = $post->ID;
        $author_id = $post->post_author; // 著者のIDを直接取得

        // 著者情報の取得をより確実な方法に変更
        $author_name = get_the_author_meta('display_name', $author_id);
        $author_url  = get_author_posts_url($author_id); // 著者のアーカイブページURL

        // アイキャッチ画像
        $thumbnail_src = has_post_thumbnail($post_id) ? get_the_post_thumbnail_url($post_id, 'full') : "https://ittekian.com/wp-content/uploads/2022/12/cropped-icon-512x512-1.png";

        $json_ld_article = [
            "@context" => "http://schema.org",
            "@type" => "BlogPosting",
            "mainEntityOfPage" => [
                "@type" => "WebPage",
                "@id" => get_permalink($post_id)
            ],
            "headline" => get_the_title($post_id),
            "image" => [
                $thumbnail_src
            ],
            "datePublished" => get_the_date('c', $post_id),
            "dateModified" => get_the_modified_date('c', $post_id),
            "author" => [
                "@type" => "Person",
                "name" => $author_name, // ここが修正ポイント
                "url" => $author_url    // これでURL警告も消えます
            ],
            "publisher" => [
                "@type" => "MedicalOrganization",
                "name" => "伝統鍼灸一滴庵",
                "logo" => [
                    "@type" => "ImageObject",
                    "url" => "https://ittekian.com/wp-content/uploads/2022/12/cropped-icon-512x512-1.png"
                ]
            ],
            "description" => get_the_excerpt($post_id)
        ];

        echo '<script type="application/ld+json">' . json_encode($json_ld_article, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . '</script>' . "\n";
    }
}
add_action('wp_head', 'add_custom_json_ld');

