{"id":367,"date":"2026-07-17T13:16:08","date_gmt":"2026-07-17T13:16:08","guid":{"rendered":"https:\/\/tools.dwiyanti.com\/blog\/?p=367"},"modified":"2026-07-17T13:16:08","modified_gmt":"2026-07-17T13:16:08","slug":"schema-item-list-2","status":"publish","type":"post","link":"https:\/\/tools.dwiyanti.com\/blog\/schema-item-list-2","title":{"rendered":"Schema Item List"},"content":{"rendered":"<pre style=\"white-space:pre-wrap;\">\nQ: bisa buat schema item list untuk functions.php untuk frontpage, single, dan page? frontpage memiliki tampilan card 6 postingan terakhir. saya menggunakan php 8.3. masukkan juga item list ini. [\n                            '@type' => 'ListItem',\n                            'position' => 1,\n                            'url' => \"https:\/\/me.dwiyanti.com\/services\",\n                            'name' => 'Dwi Yanti Services'\n                        ],\n                        [\n                            '@type' => 'ListItem',\n                            'position' => 2,\n                            'url' => \"https:\/\/me.dwiyanti.com\/seo-specialist\",\n                            'name' => 'SEO Specialist Services'\n                        ],\n                        [\n                            '@type' => 'ListItem',\n                            'position' => 3,\n                            'url' => \"https:\/\/me.dwiyanti.com\/technical-seo\",\n                            'name' => 'Technical SEO Services'\n                        ],\n                        [\n                            '@type' => 'ListItem',\n                            'position' => 4,\n                            'url' => \"https:\/\/me.dwiyanti.com\/seo-strategy\",\n                            'name' => 'SEO Strategy Services'\n                        ],\n                        [\n                            '@type' => 'ListItem',\n                            'position' => 5,\n                            'url' => \"https:\/\/me.dwiyanti.com\/seo-audit\",\n                            'name' => 'SEO Audit Services'\n                        ],\n                        [\n                            '@type' => 'ListItem',\n                            'position' => 6,\n                            'url' => \"https:\/\/me.dwiyanti.com\/cms-management-system\",\n                            'name' => 'Property Management System Development Services'\n                        ]\n-----\n<?php\n\/**\n * Generate Schema Item List untuk Frontpage, Single, dan Page\n * WordPress + PHP 8.3 Compatible\n *\/\n\n\/\/ Hook ke wp_head untuk menambahkan schema\nadd_action('wp_head', 'dwiyanti_schema_itemlist');\n\nfunction dwiyanti_schema_itemlist() {\n    \/\/ Cek kondisi halaman\n    if (is_front_page() || is_home()) {\n        dwiyanti_render_schema('frontpage');\n    } elseif (is_single()) {\n        dwiyanti_render_schema('single');\n    } elseif (is_page()) {\n        dwiyanti_render_schema('page');\n    }\n}\n\nfunction dwiyanti_render_schema($type = 'frontpage') {\n    \/\/ Base URL\n    $base_url = home_url();\n    \n    \/\/ Daftar item list yang diminta\n    $items = [\n        [\n            '@type' => 'ListItem',\n            'position' => 1,\n            'url' => \"https:\/\/me.dwiyanti.com\/services\",\n            'name' => 'Dwi Yanti Services'\n        ],\n        [\n            '@type' => 'ListItem',\n            'position' => 2,\n            'url' => \"https:\/\/me.dwiyanti.com\/seo-specialist\",\n            'name' => 'SEO Specialist Services'\n        ],\n        [\n            '@type' => 'ListItem',\n            'position' => 3,\n            'url' => \"https:\/\/me.dwiyanti.com\/technical-seo\",\n            'name' => 'Technical SEO Services'\n        ],\n        [\n            '@type' => 'ListItem',\n            'position' => 4,\n            'url' => \"https:\/\/me.dwiyanti.com\/seo-strategy\",\n            'name' => 'SEO Strategy Services'\n        ],\n        [\n            '@type' => 'ListItem',\n            'position' => 5,\n            'url' => \"https:\/\/me.dwiyanti.com\/seo-audit\",\n            'name' => 'SEO Audit Services'\n        ],\n        [\n            '@type' => 'ListItem',\n            'position' => 6,\n            'url' => \"https:\/\/me.dwiyanti.com\/cms-management-system\",\n            'name' => 'Property Management System Development Services'\n        ]\n    ];\n\n    \/\/ Schema dasar\n    $schema = [\n        '@context' => 'https:\/\/schema.org',\n        '@type' => 'ItemList',\n        'itemListElement' => $items\n    ];\n\n    \/\/ Tambahan untuk frontpage - 6 postingan terakhir\n    if ($type === 'frontpage') {\n        $recent_posts = get_posts([\n            'numberposts' => 6,\n            'post_status' => 'publish',\n            'orderby' => 'date',\n            'order' => 'DESC'\n        ]);\n\n        if (!empty($recent_posts)) {\n            $position = count($items) + 1;\n            foreach ($recent_posts as $post) {\n                $schema['itemListElement'][] = [\n                    '@type' => 'ListItem',\n                    'position' => $position,\n                    'url' => get_permalink($post->ID),\n                    'name' => esc_html($post->post_title)\n                ];\n                $position++;\n            }\n        }\n    }\n\n    \/\/ Tambahan untuk single - menampilkan postingan terkait\n    if ($type === 'single') {\n        global $post;\n        $categories = wp_get_post_categories($post->ID, ['fields' => 'ids']);\n        \n        if (!empty($categories)) {\n            $related_posts = get_posts([\n                'category__in' => $categories,\n                'numberposts' => 5,\n                'post__not_in' => [$post->ID],\n                'post_status' => 'publish'\n            ]);\n\n            if (!empty($related_posts)) {\n                $position = count($items) + 1;\n                foreach ($related_posts as $related) {\n                    $schema['itemListElement'][] = [\n                        '@type' => 'ListItem',\n                        'position' => $position,\n                        'url' => get_permalink($related->ID),\n                        'name' => esc_html($related->post_title)\n                    ];\n                    $position++;\n                }\n            }\n        }\n    }\n\n    \/\/ Tambahan untuk page - menampilkan subpages jika ada\n    if ($type === 'page') {\n        global $post;\n        $child_pages = get_posts([\n            'post_type' => 'page',\n            'post_parent' => $post->ID,\n            'numberposts' => -1,\n            'post_status' => 'publish',\n            'orderby' => 'menu_order',\n            'order' => 'ASC'\n        ]);\n\n        if (!empty($child_pages)) {\n            $position = count($items) + 1;\n            foreach ($child_pages as $child) {\n                $schema['itemListElement'][] = [\n                    '@type' => 'ListItem',\n                    'position' => $position,\n                    'url' => get_permalink($child->ID),\n                    'name' => esc_html($child->post_title)\n                ];\n                $position++;\n            }\n        }\n    }\n\n    \/\/ Render schema dengan JSON-LD\n    echo '<script type=\"application\/ld+json\">' . PHP_EOL;\n    echo json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);\n    echo PHP_EOL . '<\/script>' . PHP_EOL;\n}\n\n\/**\n * Optional: Tambahkan filter untuk memodifikasi schema\n *\/\nadd_filter('dwiyanti_schema_items', 'dwiyanti_custom_schema_items', 10, 2);\n\nfunction dwiyanti_custom_schema_items($items, $type) {\n    \/\/ Anda bisa memodifikasi $items berdasarkan $type di sini\n    return $items;\n}\n\n\/**\n * Utility: Clear cache jika menggunakan cache plugin\n *\/\nadd_action('save_post', 'dwiyanti_clear_schema_cache');\nfunction dwiyanti_clear_schema_cache() {\n    \/\/ Clear cache jika menggunakan plugin cache\n    if (function_exists('wp_cache_clear_cache')) {\n        wp_cache_clear_cache();\n    }\n}\n\n<\/pre>","protected":false},"excerpt":{"rendered":"Q: bisa buat schema item list untuk functions.php untuk frontpage, single, dan page? frontpage memiliki tampilan card 6 postingan terakhir. saya menggunakan php 8.3. masukkan juga item list ini. [ &#8216;@type&#8217; => &#8216;ListItem&#8217;, &#8216;position&#8217; => 1, &#8216;url&#8217; => &#8220;https:\/\/me.dwiyanti.com\/services&#8221;, &#8216;name&#8217; => &#8216;Dwi Yanti Services&#8217; ], [ &#8216;@type&#8217; => &#8216;ListItem&#8217;, &#8216;position&#8217; => 2, &#8216;url&#8217; => &#8220;https:\/\/me.dwiyanti.com\/seo-specialist&#8221;, [&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-367","post","type-post","status-publish","format-standard","hentry","category-schema"],"_links":{"self":[{"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/posts\/367","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=367"}],"version-history":[{"count":1,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":368,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/posts\/367\/revisions\/368"}],"wp:attachment":[{"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tools.dwiyanti.com\/blog\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}