Compare commits
2 Commits
42704b9cb0
...
4c26c1444f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c26c1444f | ||
|
|
76c5115d0a |
|
|
@ -132,27 +132,48 @@ function count_user_articles($userID, $postType)
|
||||||
function build_sommaire_from_content($postID)
|
function build_sommaire_from_content($postID)
|
||||||
{
|
{
|
||||||
$blocks = parse_blocks(get_the_content($postID));
|
$blocks = parse_blocks(get_the_content($postID));
|
||||||
$titleBlocks = array_filter(
|
|
||||||
$blocks,
|
|
||||||
function ($block) {
|
|
||||||
// Vérifier si c'est un bloc heading
|
|
||||||
if ($block['blockName'] !== 'core/heading') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extraire le niveau depuis le HTML si les attributs sont vides
|
// Collecter les h2 au niveau racine ET dans les innerBlocks immédiats (profondeur 1)
|
||||||
if (empty($block['attrs']['level'])) {
|
$titleBlocks = [];
|
||||||
// Chercher seulement h2 dans le HTML
|
|
||||||
if (preg_match('/<h2[^>]*>/i', $block['innerHTML'], $matches)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Utiliser le niveau des attributs s'il existe
|
$checkIsH2 = function ($block) {
|
||||||
return $block['attrs']['level'] === 2;
|
if (!is_array($block) || empty($block['blockName'])) return false;
|
||||||
|
if ($block['blockName'] !== 'core/heading') return false;
|
||||||
|
|
||||||
|
// Exclure explicitement les titres marqués avec la classe not-in-index
|
||||||
|
if (!empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'not-in-index') !== false) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
);
|
if (!empty($block['innerHTML']) && preg_match('/class="[^"]*not-in-index[^"]*"/i', $block['innerHTML'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Utiliser le niveau depuis les attributs si présent
|
||||||
|
if (!empty($block['attrs']['level'])) {
|
||||||
|
return intval($block['attrs']['level']) === 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sinon, détecter <h2> dans le HTML du bloc
|
||||||
|
if (!empty($block['innerHTML']) && preg_match('/<h2[^>]*>/i', $block['innerHTML'])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach ($blocks as $block) {
|
||||||
|
if ($checkIsH2($block)) {
|
||||||
|
$titleBlocks[] = $block;
|
||||||
|
}
|
||||||
|
// Parcours des enfants immédiats uniquement
|
||||||
|
if (!empty($block['innerBlocks']) && is_array($block['innerBlocks'])) {
|
||||||
|
foreach ($block['innerBlocks'] as $child) {
|
||||||
|
if ($checkIsH2($child)) {
|
||||||
|
$titleBlocks[] = $child;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$outputIndex = [];
|
$outputIndex = [];
|
||||||
|
|
||||||
|
|
@ -167,7 +188,7 @@ function build_sommaire_from_content($postID)
|
||||||
|
|
||||||
if ($level !== 2) continue;
|
if ($level !== 2) continue;
|
||||||
|
|
||||||
$anchor = $block['attrs']['idName'] ?? sanitize_title($title);
|
$anchor = $block['attrs']['idName'] ?? ($block['attrs']['anchor'] ?? sanitize_title($title));
|
||||||
|
|
||||||
// Ajouter un préfixe si l'ancre commence par un chiffre
|
// Ajouter un préfixe si l'ancre commence par un chiffre
|
||||||
if (!empty($anchor) && preg_match('/^[0-9]/', $anchor)) {
|
if (!empty($anchor) && preg_match('/^[0-9]/', $anchor)) {
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,17 @@ $issue_related_articles = get_field('articles', $revueID);
|
||||||
<section class="table-matieres">
|
<section class="table-matieres">
|
||||||
<h3 class="content-tab__title">Table des matières</h3>
|
<h3 class="content-tab__title">Table des matières</h3>
|
||||||
<ul class="post-grid__list article-grid__list">
|
<ul class="post-grid__list article-grid__list">
|
||||||
<?php foreach ($issue_related_articles as $article) : ?>
|
<?php if ($issue_related_articles && is_array($issue_related_articles)) : ?>
|
||||||
<?php get_template_part('template-parts/articles/card-article', null, array(
|
<?php foreach ($issue_related_articles as $article) : ?>
|
||||||
|
<?php get_template_part('template-parts/articles/card-article', null, array(
|
||||||
|
|
||||||
'date' => $article->post_date,
|
'date' => $article->post_date,
|
||||||
'image' => get_the_post_thumbnail_url($article->ID),
|
'image' => get_the_post_thumbnail_url($article->ID),
|
||||||
'link' => get_the_permalink($article->ID),
|
'link' => get_the_permalink($article->ID),
|
||||||
'ID' => $article->ID,
|
'ID' => $article->ID,
|
||||||
'showAuthors' => true,
|
'showAuthors' => true,
|
||||||
)); ?>
|
)); ?>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
</ul>
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
Loading…
Reference in New Issue
Block a user