Yazılarda Otomatik İçindekiler Kodu
function generate_toc($content) { // İçerikte H2 ve H3 başlıkları ara preg_match_all('/<h[2-3]>(.*?)<\/h[2-3]>/', $content, $matches); if (empty($matches[0])) { return $content; // Eğer başlık yoksa orijinal içeriği döndür } // TOC HTML kodunu oluştur $toc = '<div class="toc"><h2>İçindekiler</h2><ul>'; foreach ($matches[1] as $index => $title) { $anchor = 'toc-' . $index; // Başlığa bir id ekleyerek bağlantı oluştur $content = str_replace($matches[0][$index], '<h' . ($index + 2) . ' id="' . $anchor . '">' . $title . '</h' . ($index + 2) . '>', $content); // TOC içeriğini oluştur $toc .= '<li><a href="#' . $anchor . '">' . $title . '</a></li>'; } $toc .= '</ul></div>'; // TOC'yi içerikte ilk paragraf sonrasına ekle $content = preg_replace('/<p>/', $toc . '<p>', $content, 1); return $content; } function add_toc_to_content($content) { if (is_single() && in_the_loop() && is_main_query()) { return generate_toc($content); } return $content; } add_filter('the_content', 'add_toc_to_content');Sadece Özel İçerik Tipinde Göstermek İçin (Örnek: TELEFON) Kodfunction generate_toc($content) { // İçerikte H2 ve H3 başlıkları ara preg_match_all('/<h[2-3]>(.*?)<\/h[2-3]>/', $content, $matches); if (empty($matches[0])) { return $content; // Eğer başlık yoksa orijinal içeriği döndür } // TOC HTML kodunu oluştur $toc = '<div class="toc"><h2>İçindekiler</h2><ul>'; foreach ($matches[1] as $index => $title) { $anchor = 'toc-' . $index; // Başlığa bir id ekleyerek bağlantı oluştur $content = str_replace($matches[0][$index], '<h' . ($index + 2) . ' id="' . $anchor . '">' . $title . '</h' . ($index + 2) . '>', $content); // TOC içeriğini oluştur $toc .= '<li><a href="#' . $anchor . '">' . $title . '</a></li>'; } $toc .= '</ul></div>'; // TOC'yi içerikte ilk paragraf sonrasına ekle $content = preg_replace('/<p>/', $toc . '<p>', $content, 1); return $content; } function add_toc_to_content($content) { if (is_singular('telefon') && in_the_loop() && is_main_query()) { return generate_toc($content); } return $content; } add_filter('the_content', 'add_toc_to_content');