I have a WordPress site, and I generate links using a function in the function.php file. In the single.php file, I use to display the function on the main section of the targeted pages.
Currently, the links appear after the footer. My question is, how can I make the links appear after the first heading on the targeted pages?
The function
function generate_internal_links($pageSlug, $pageTitle, $keywords, $currentPageURL)
{
$internalLinks = '';
// Check if the current page URL matches the provided condition
if ($currentPageURL === $pageSlug) {
$foundH3 = false; // Flag to track if the first <h3> tag is found
foreach ($keywords as $keyword) {
$slug = strtolower($keyword);
$slug = str_replace(['é', 'è'], 'e', $slug);
$slug = preg_replace('/[^a-zA-Z0-9]+/', '-', $slug);
$link = 'https://sg-plombier.be/' . $slug . '/';
$internalLinks .= '<a href="' . $link . '">' . $keyword . '</a>, ';
// Check if the <h3> tag is found
if (!$foundH3 && strpos($keyword, '<h3>') !== false) {
$foundH3 = true;
$internalLinks = rtrim($internalLinks, ', ');
// Apply the function before the first <h3> tag
$internalLinks = $pageTitle . ': ' . $internalLinks;
}
}
if (!$foundH3) {
// If no <h3> tag is found, apply the function at the end
$internalLinks = rtrim($internalLinks, ', ');
$internalLinks = $pageTitle . ': ' . $internalLinks;
}
}
return $internalLinks;
}
// Get the current page URL
$currentPageURL = $_SERVER['REQUEST_URI'];
// Generate the internal links based on the conditions
$debouchageBruxellesKeywords = [
'Débouchage Molenbeek',
'Débouchage Jette',
'Débouchage Laeken',
'Débouchage Saint-Gilles',
'Débouchage Watermael-Boitsfort',
'Débouchage Berchem-Sainte-Agathe',
'Débouchage Haren',
'Débouchage Ganshoren',
'Débouchage Etterbeek',
'Débouchage Uccle',
'Débouchage Schaerbeek',
'Débouchage Auderghem',
'Débouchage Anderlecht',
'Débouchage Forest',
'Débouchage Saint-Josse',
'Débouchage Woluwe-Saint-Lambert',
'Débouchage Neder-Over-Heembeek',
'Débouchage Evere',
'Débouchage Koekelberg',
'Débouchage Ixelles',
];
$debouchageBruxellesLink = generate_internal_links('/debouchage-bruxelles/', 'Débouchage Bruxelles', $debouchageBruxellesKeywords, $currentPageURL);
$debouchageLiegeKeywords = [
'Débouchage Ans',
'Débouchage Awans',
'Débouchage Aywaille',
'Débouchage Bassenge',
'Débouchage Beyne-Heusay',
'Débouchage Blegny',
'Débouchage Comblain-au-Pont',
'Débouchage Flémalle',
'Débouchage Fléron',
'Débouchage Soumagne',
'Débouchage Sprimont',
'Débouchage Chaudfontaine',
'Débouchage Grâce-Hollogne',
'Débouchage Herstal',
'Débouchage Juprelle',
'Débouchage Neupré',
'Débouchage Saint-Nicolas',
'Débouchage Seraing',
'Débouchage Trooz',
'Débouchage Visé',
'Débouchage Dalhem',
'Débouchage Esneux',
'Débouchage Oupeye',
];
$debouchageLiegeLink = generate_internal_links('/debouchage-liege/', 'Débouchage Liège', $debouchageLiegeKeywords, $currentPageURL);
$smallCitiesLiegeKeywords = [
'Débouchage Huy',
'Débouchage Verviers',
'Débouchage Waremme',
];
$smallCitiesLiegeLink = generate_internal_links('/debouchage-liege/', 'Débouchage Liège', $smallCitiesLiegeKeywords, $currentPageURL);
$debouchageHuyKeywords = [
'Débouchage Anthisnes',
'Débouchage Burdinne',
'Débouchage Clavier',
'Débouchage Engis',
'Débouchage Ferrières',
'Débouchage Villers-le-Bouillet',
'Débouchage Hamoir',
'Débouchage Héron',
'Débouchage Amay',
'Débouchage Grâce-Marchin',
'Débouchage Modave',
'Débouchage Nandrin',
'Débouchage Ouffet',
'Débouchage Tinlot',
'Débouchage Verlaine',
'Débouchage Wanze',
];
$debouchageHuyLink = generate_internal_links('/debouchage-huy/', 'Débouchage Huy', $debouchageHuyKeywords, $currentPageURL);
$debouchageVerviersKeywords = [
'Débouchage Amblève (Amel)',
'Débouchage Aubel',
'Débouchage Baelen',
'Débouchage Bullange',
'Débouchage Burg-Reuland',
'Débouchage Butgenbach',
'Débouchage Dison',
'Débouchage Saint-Vith',
'Débouchage Spa',
'Débouchage Stavelot',
'Débouchage Stoumont',
'Débouchage Theux',
'Débouchage Eupen',
'Débouchage Herve',
'Débouchage La Calamine',
'Débouchage Lierneux',
'Débouchage Limbourg',
'Débouchage Lontzen',
'Débouchage Malmedy',
'Débouchage Olne',
'Débouchage Plombières',
'Débouchage Raeren',
'Débouchage Pepinster',
'Débouchage Waimes',
'Débouchage Thimister-Clermont',
'Débouchage Trois-Ponts',
'Débouchage Welkenraedt',
];
$debouchageVerviersLink = generate_internal_links('/debouchage-verviers/', 'Débouchage Verviers', $debouchageVerviersKeywords, $currentPageURL);
$debouchageWaremmeKeywords = [
'Débouchage Berloz',
'Débouchage Braives',
'Débouchage Crisnée',
'Débouchage Donceel',
'Débouchage Faimes',
'Débouchage Fexhe-le-Haut-Clocher',
'Débouchage Geer',
'Débouchage Hannut',
'Débouchage Lincent',
'Débouchage Oreye',
'Débouchage Remicourt',
'Débouchage Wasseiges',
'Débouchage Saint-Georges-sur-Meuse',
];
$debouchageWaremmeLink = generate_internal_links('/debouchage-waremme/', 'Débouchage Waremme', $debouchageWaremmeKeywords, $currentPageURL);
$debouchageBrabantKeywords = [
'Débouchage Wavre',
'Débouchage Beauvechain',
'Débouchage Braine-lalleud',
'Débouchage Braine-le-Chateau',
'Débouchage Chastre',
'Débouchage Chaumont-Gistoux',
'Débouchage Court-Saint-Étienne',
'Débouchage Genappe',
'Débouchage Grez-Doiceau',
'Débouchage Rixensart',
'Débouchage Tubize',
'Débouchage Villers-la-Ville',
'Débouchage Walhain',
'Débouchage Hélécine',
'Débouchage Incourt',
'Débouchage Ittre',
'Débouchage Jodoigne',
'Débouchage La Hulpe',
'Débouchage Lasne',
'Débouchage Mont-Saint-Guibert',
'Débouchage Nivelles',
'Débouchage Orp-Jauche',
'Débouchage Ottignies-Louvain-la-Neuve',
'Débouchage Perwez',
'Débouchage Ramillies',
'Débouchage Rebecq',
'Débouchage Waterloo',
];
$debouchageBrabantLink = generate_internal_links('/debouchage-brabant-wallon/', 'Débouchage brabant wallon', $debouchageBrabantKeywords, $currentPageURL);
$debouchageNamurKeywords = [
'Débouchage Andenne',
'Débouchage Assesse',
'Débouchage Éghezée',
'Débouchage Fernelmont',
'Débouchage Floreffe',
'Débouchage Fosses-la-Ville',
'Débouchage Gembloux',
'Débouchage La Bruyère',
'Débouchage Ohey',
'Débouchage Profondeville',
'Débouchage Sambreville',
'Débouchage Sombreffe',
'Débouchage Gesves',
'Débouchage Mettet',
'Débouchage Jemeppe-sur-Sambre',
];
$debouchageNamurLink = generate_internal_links('/debouchage-namur/', 'Débouchage Namur', $debouchageNamurKeywords, $currentPageURL);
$smallCitiesNamurKeywords = [
'Débouchage Dinant',
'Débouchage Philippeville',
];
$smallCitiesNamurLink = generate_internal_links('/debouchage-namur/', 'Débouchage Namur', $smallCitiesNamurKeywords, $currentPageURL);
$debouchageDinantKeywords = [
'Débouchage Beauraing',
'Débouchage Bièvre',
'Débouchage Ciney',
'Débouchage Dinant',
'Débouchage Gedinne',
'Débouchage Hamois',
'Débouchage Havelange',
'Débouchage Houyet',
'Débouchage Yvoir',
'Débouchage Hastière',
'Débouchage Onhaye',
'Débouchage Rochefort',
'Débouchage Somme-Leuze',
'Débouchage Vresse-sur-Semois',
];
$debouchageDinantLink = generate_internal_links('/debouchage-dinant/', 'Débouchage Dinant', $debouchageDinantKeywords, $currentPageURL);
$debouchagePhilippevilleKeywords = [
'Débouchage Cerfontaine',
'Débouchage Couvin',
'Débouchage Doische',
'Débouchage Florennes',
'Débouchage Walcourt',
'Débouchage Viroinval',
];
$debouchagePhilippevilleLink = generate_internal_links('/debouchage-philippeville/', 'Débouchage Philippeville', $debouchagePhilippevilleKeywords, $currentPageURL);
$debouchageHainautKeywords = [
'Débouchage Mons',
'Débouchage Boussu',
'Débouchage Dour',
'Débouchage Frameries',
'Débouchage Jurbise',
'Débouchage Lens',
'Débouchage Quaregnon',
'Débouchage Quévy',
'Débouchage Quiévrain',
'Débouchage Saint-Ghislain',
'Débouchage Hensies',
'Débouchage Colfontaine',
'Débouchage Honnelles',
];
$debouchageHainautLink = generate_internal_links('/debouchage-hainaut/', 'Débouchage Hainaut', $debouchageHainautKeywords, $currentPageURL);
$debouchageHainautKeywords = [
'Débouchage Mons',
'Débouchage Ath',
'Débouchage Charleroi',
'Débouchage La Louvière',
'Débouchage Soignies',
'Débouchage Thuin',
'Débouchage Tournai',
];
$smallCitiesHainautLink = generate_internal_links('/debouchage-hainaut/', 'Débouchage Hainaut', $debouchageHainautKeywords, $currentPageURL);
$debouchageMonsKeywords = [
'Débouchage Hainaut',
'Débouchage Boussu',
'Débouchage Dour',
'Débouchage Frameries',
'Débouchage Jurbise',
'Débouchage Lens',
'Débouchage Quaregnon',
'Débouchage Quévy',
'Débouchage Quiévrain',
'Débouchage Saint-Ghislain',
'Débouchage Hensies',
'Débouchage Colfontaine',
'Débouchage Honnelles',
];
$debouchageMonsLink = generate_internal_links('/debouchage-mons/', 'Débouchage Mons', $debouchageMonsKeywords, $currentPageURL);
$debouchageAthKeywords = [
'Débouchage Belœil',
'Débouchage Bernissart',
'Débouchage Brugelette',
'Débouchage Chièvres',
'Débouchage Enghien',
'Débouchage Ellezelles',
'Débouchage Flobecq',
'Débouchage Frasnes-lez-Anvaing',
'Débouchage Lessines',
'Débouchage Silly',
];
$debouchageAthLink = generate_internal_links('/debouchage-ath/', 'Débouchage Ath', $debouchageAthKeywords, $currentPageURL);
$debouchageCharleroiKeywords = [
'Débouchage Aiseau-Presles',
'Débouchage Chapelle-lez-Herlaimont',
'Débouchage Courcelles',
'Débouchage Fleurus',
'Débouchage Gerpinnes',
'Débouchage Les Bons Villers',
'Débouchage Montigny-le-Tilleul',
'Débouchage Farciennes',
'Débouchage Fontaine-l\'Évêque',
'Débouchage Châtelet',
'Débouchage Pont-à-Celles',
];
$debouchageCharleroiLink = generate_internal_links('/debouchage-charleroi/', 'Débouchage Charleroi', $debouchageCharleroiKeywords, $currentPageURL);
$debouchageLaLouvièreKeywords = [
'Débouchage Binche',
'Débouchage Estinnes',
'Débouchage Morlanwelz',
];
$debouchageLaLouvièreLink = generate_internal_links('/debouchage-la-louviere/', 'Débouchage La Louvière', $debouchageLaLouvièreKeywords, $currentPageURL);
$debouchageSoigniesKeywords = [
'Débouchage Braine-le-Comte',
'Débouchage Écaussinnes',
'Débouchage Le Rœulx',
'Débouchage Manage',
'Débouchage Seneffe',
];
$debouchageSoigniesLink = generate_internal_links('/debouchage-soignies/', 'Débouchage Soignies', $debouchageSoigniesKeywords, $currentPageURL);
$debouchageThuinKeywords = [
'Débouchage Anderlues',
'Débouchage Beaumont',
'Débouchage Chimay',
'Débouchage Erquelinnes',
'Débouchage Froidchapelle',
'Débouchage Ham-sur-Heure-Nalinnes',
'Débouchage Lobbes',
'Débouchage Merbes-le-Château',
'Débouchage Momignies',
'Débouchage Sivry-Rance',
];
$debouchageThuinLink = generate_internal_links('/debouchage-thuin/', 'Débouchage Thuin', $debouchageThuinKeywords, $currentPageURL);
$debouchageTournaiKeywords = [
'Débouchage Antoing',
'Débouchage Celles',
'Débouchage Comines-Warneton',
'Débouchage Estaimpuis',
'Débouchage Leuze-en-Hainaut',
'Débouchage Mont-de-l\'Enclus',
'Débouchage Mouscron',
'Débouchage Pecq',
'Débouchage Péruwelz',
'Débouchage Rumes',
];
$debouchageTournaiLink = generate_internal_links('/debouchage-tournai/', 'Débouchage Tournai', $debouchageTournaiKeywords, $currentPageURL);
// Output the generated links
function display_internal_links() {
$output = '';
global $debouchageLiegeLink, $smallCitiesLiegeLink, $debouchageHuyLink, $debouchageVerviersLink, $debouchageWaremmeLink, $debouchageBruxellesLink, $debouchageBrabantLink, $debouchageNamurLink, $smallCitiesNamurLink, $debouchageDinantLink, $debouchagePhilippevilleLink, $debouchageHainautLink, $smallCitiesHainautLink, $debouchageMonsLink, $debouchageAthLink, $debouchageCharleroiLink, $debouchageLaLouvièreLink, $debouchageSoigniesLink, $debouchageThuinLink, $debouchageTournaiLink;
if (!empty($debouchageLiegeLink)) {
$output .= $debouchageLiegeLink;
}
if (!empty($smallCitiesLiegeLink)) {
$output .= $smallCitiesLiegeLink;
}
if (!empty($debouchageHuyLink)) {
$output .= $debouchageHuyLink;
}
if (!empty($debouchageVerviersLink)) {
$output .= $debouchageVerviersLink;
}
if (!empty($debouchageWaremmeLink)) {
$output .= $debouchageWaremmeLink;
}
if (!empty($debouchageBruxellesLink)) {
$output .= $debouchageBruxellesLink;
}
if (!empty($debouchageBrabantLink)) {
$output .= $debouchageBrabantLink;
}
if (!empty($debouchageNamurLink)) {
$output .= $debouchageNamurLink;
}
if (!empty($smallCitiesNamurLink)) {
$output .= $smallCitiesNamurLink;
}
if (!empty($debouchageDinantLink)) {
$output .= $debouchageDinantLink;
}
if (!empty($debouchagePhilippevilleLink)) {
$output .= $debouchagePhilippevilleLink;
}
if (!empty($debouchageHainautLink)) {
$output .= $debouchageHainautLink;
}
if (!empty($smallCitiesHainautLink)) {
$output .= $smallCitiesHainautLink;
}
if (!empty($debouchageMonsLink)) {
$output .= $debouchageMonsLink;
}
if (!empty($debouchageAthLink)) {
$output .= $debouchageAthLink;
}
if (!empty($debouchageCharleroiLink)) {
$output .= $debouchageCharleroiLink;
}
if (!empty($debouchageLaLouvièreLink)) {
$output .= $debouchageLaLouvièreLink;
}
if (!empty($debouchageSoigniesLink)) {
$output .= $debouchageSoigniesLink;
}
if (!empty($debouchageThuinLink)) {
$output .= $debouchageThuinLink;
}
if (!empty($debouchageTournaiLink)) {
$output .= $debouchageTournaiLink;
}
return $output;
}
how can I make the links appear after the first heading on the targeted pages?