{"id":364,"date":"2026-07-16T06:56:39","date_gmt":"2026-07-16T06:56:39","guid":{"rendered":"https:\/\/tools.dwiyanti.com\/blog\/?p=364"},"modified":"2026-07-16T06:56:40","modified_gmt":"2026-07-16T06:56:40","slug":"schema-collection-page","status":"publish","type":"post","link":"https:\/\/tools.dwiyanti.com\/blog\/schema-collection-page","title":{"rendered":"Schema Collection Page"},"content":{"rendered":"<pre style=\"white-space:pre-wrap;\">\n\n\/* Schema CollectionPage untuk homepage statis yang menampilkan postingan terbaru *\/\nadd_action( 'wp_head', 'static_homepage_with_posts_schema' );\nfunction static_homepage_with_posts_schema() {\n    \/\/ Hanya di front page (halaman statis yang dijadikan beranda)\n    if ( ! is_front_page() ) {\n        return;\n    }\n\n    $site_name = get_bloginfo( 'name' );\n    $site_url = home_url();\n    $site_description = get_bloginfo( 'description' );\n    $homepage_title = get_optimized_meta_title();\n\n    \/\/ Ambil 6 postingan terbaru\n    $recent_posts = get_posts( array(\n        'numberposts' => 6,\n        'post_status' => 'publish',\n        'post_type'   => 'post'\n    ) );\n\n    \/\/ Siapkan array itemListElement untuk blog posts\n    $item_list = array();\n    $position = 1;\n\n    foreach ( $recent_posts as $post ) {\n        setup_postdata( $post );\n\n        \/\/ ===== AMBIL GAMBAR DARI KONTEN (PRIORITAS UTAMA) =====\n        $post_image = '';\n        $content = get_post_field( 'post_content', $post->ID );\n\n        \/\/ Cari gambar pertama dari konten\n        preg_match( '\/<img.+src=[\\'\"]([^\\'\"]+)[\\'\"].*>\/i', $content, $matches );\n        if ( ! empty( $matches[1] ) ) {\n            $post_image = $matches[1];\n        }\n\n        \/\/ ===== FALLBACK 1: Jika tidak ada gambar di konten, ambil dari thumbnail\/feature image =====\n        if ( empty( $post_image ) ) {\n            $thumbnail_id = get_post_thumbnail_id( $post->ID );\n            if ( $thumbnail_id ) {\n                $thumbnail_url = wp_get_attachment_image_url( $thumbnail_id, 'full' );\n                if ( $thumbnail_url ) {\n                    $post_image = $thumbnail_url;\n                }\n            }\n        }\n\n        \/\/ ===== FALLBACK 2: Jika masih tidak ada, gunakan gambar default =====\n        if ( empty( $post_image ) ) {\n            $post_image = $site_url . '\/wp-content\/themes\/lifestyle\/assets\/images\/DwiYanti.jpeg';\n        }\n\n        \/\/ Buat ImageObject untuk setiap postingan\n        $image_object = array(\n            '@type' => 'ImageObject',\n            'url' => $post_image,\n            'width' => 800,\n            'height' => 600\n        );\n\n        \/\/ Coba dapatkan dimensi gambar sebenarnya (jika gambar lokal)\n        if ( strpos( $post_image, home_url() ) !== false ) {\n            $attachment_id = attachment_url_to_postid( $post_image );\n            if ( $attachment_id ) {\n                $metadata = wp_get_attachment_metadata( $attachment_id );\n                if ( $metadata && isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {\n                    $image_object['width'] = $metadata['width'];\n                    $image_object['height'] = $metadata['height'];\n                }\n            }\n        }\n        \/\/ ===== END AMBIL GAMBAR =====\n\n        $item_list[] = array(\n            '@type' => 'ListItem',\n            'position' => $position,\n            'url' => get_permalink( $post->ID ),\n            'name' => get_the_title( $post->ID ),\n            'description' => wp_trim_words( get_the_excerpt( $post ), 20 ),\n            'image' => $image_object\n        );\n        $position++;\n    }\n    wp_reset_postdata();\n\n    \/\/ ===== GAMBAR UNTUK COLLECTIONPAGE (opsional) =====\n    $collection_image = '';\n\n    \/\/ Coba ambil dari konten postingan pertama\n    if ( ! empty( $recent_posts ) ) {\n        $first_post = $recent_posts[0];\n        $content_first = get_post_field( 'post_content', $first_post->ID );\n        preg_match( '\/<img.+src=[\\'\"]([^\\'\"]+)[\\'\"].*>\/i', $content_first, $matches_first );\n        if ( ! empty( $matches_first[1] ) ) {\n            $collection_image = $matches_first[1];\n        }\n    }\n\n    \/\/ Jika tidak ada, coba dari thumbnail postingan pertama\n    if ( empty( $collection_image ) && ! empty( $recent_posts ) ) {\n        $first_post = $recent_posts[0];\n        $thumbnail_id = get_post_thumbnail_id( $first_post->ID );\n        if ( $thumbnail_id ) {\n            $collection_image = wp_get_attachment_image_url( $thumbnail_id, 'full' );\n        }\n    }\n\n    \/\/ Jika masih tidak ada, coba dari logo situs\n    if ( empty( $collection_image ) ) {\n        $custom_logo_id = get_theme_mod( 'custom_logo' );\n        if ( $custom_logo_id ) {\n            $collection_image = wp_get_attachment_image_url( $custom_logo_id, 'full' );\n        }\n    }\n\n    \/\/ Fallback terakhir: gambar default\n    if ( empty( $collection_image ) ) {\n        $collection_image = $site_url . '\/wp-content\/themes\/lifestyle\/assets\/images\/DwiYanti.jpeg';\n    }\n\n    $collection_image_object = array(\n        '@type' => 'ImageObject',\n        'url' => $collection_image,\n        'width' => 1200,\n        'height' => 630\n    );\n\n    \/\/ Coba dapatkan dimensi gambar collection\n    if ( strpos( $collection_image, home_url() ) !== false ) {\n        $attachment_id = attachment_url_to_postid( $collection_image );\n        if ( $attachment_id ) {\n            $metadata = wp_get_attachment_metadata( $attachment_id );\n            if ( $metadata && isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {\n                $collection_image_object['width'] = $metadata['width'];\n                $collection_image_object['height'] = $metadata['height'];\n            }\n        }\n    }\n    \/\/ ===== END GAMBAR COLLECTIONPAGE =====\n\n    $schema_collection = array(\n        '@context' => 'https:\/\/schema.org',\n        '@type' => 'CollectionPage',\n        '@id' => $site_url . '\/#collectionpage',\n        'url' => $site_url,\n        'name' => $homepage_title ? $homepage_title : $site_name,\n        'description' => $site_description ? $site_description : 'Beranda ' . $site_name,\n        'about' => array(\n            '@type' => 'WebPage',\n            'name' => $homepage_title\n        ),\n        'isPartOf' => array(\n            '@type' => 'WebSite',\n            '@id' => $site_url . '\/#website',\n            'name' => $site_name,\n            'url' => $site_url\n        ),\n        'mainEntity' => array(\n            '@type' => 'ItemList',\n            'itemListElement' => $item_list,\n            'numberOfItems' => count( $recent_posts )\n        )\n    );\n\n    \/\/ Output JSON-LD\n    echo '<script type=\"application\/ld+json\">' . \"\\n\";\n    echo json_encode( $schema_collection, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );\n    echo \"\\n\" . '<\/script>' . \"\\n\";\n}\n\/\/ END Schema CollectionPage\n<\/pre>","protected":false},"excerpt":{"rendered":"\/* Schema CollectionPage untuk homepage statis yang menampilkan postingan terbaru *\/ add_action( &#8216;wp_head&#8217;, &#8216;static_homepage_with_posts_schema&#8217; ); function static_homepage_with_posts_schema() { \/\/ Hanya di front page (halaman statis yang dijadikan beranda) if ( ! is_front_page() ) { return; } $site_name = get_bloginfo( &#8216;name&#8217; ); $site_url = home_url(); $site_description = get_bloginfo( &#8216;description&#8217; ); $homepage_title = get_optimized_meta_title(); \/\/ Ambil 6 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-364","post","type-post","status-publish","format-standard","hentry","category-schema"],"_links":{"self":[{"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/posts\/364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/comments?post=364"}],"version-history":[{"count":1,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/posts\/364\/revisions"}],"predecessor-version":[{"id":366,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/posts\/364\/revisions\/366"}],"wp:attachment":[{"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/media?parent=364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/categories?post=364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/tags?post=364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}