WordPress Custom Partials mit WP-Block-Shortcode einbinden (HowTo)
Version vom 30. Juni 2021, 08:48 Uhr von Sebastian.kalms (Diskussion | Beiträge) (→Vorangegangene(r) Prozess(e))
Allgemeines
TBD
Vorangegangene(r) Prozess(e)
TBD via Link zu anderem HowTo
Arbeitsschritte
Schritt 1: Funktion im Theme anlegen
- child_theme/ --> Ordner "php" erstellen, wenn noch nicht vorhanden
- child_theme/php/ --> Datei "_shortcodes.php" erstellen
- child_theme/php/_shortcodes.php --> folgendes Snippet integrieren
<?php defined( 'ABSPATH' ) or die();
//[theme_partial partial=name_of_the_partial]
function shortcodeThemePartial( $atts ) {
if (!isset($atts['partial'])) return;
$partial = trim($atts['partial']);
if ($partial == '') return;
if (!preg_match('/^[a-z0-9_\-]+$/i', $partial)) return;
$partial_file = get_stylesheet_directory().'/partials/shortcode/'.$partial.'.php';
if (!file_exists($partial_file)) return;
ob_start();
include($partial_file);
$content = ob_get_clean();
return $content;
}
add_shortcode( 'theme_partial', 'shortcodeThemePartial' );
Schritt 2: Datei mit Funktion in der functions.php laden
- child_theme/functions.php --> folgendes Snippet integrieren
include 'php/_shortcodes.php';
Schritt 3: Struktur für Partials im Theme anlegen
- dient der Ablage der einzelnen Partials
- child_theme/ --> Ordner "partials" anlegen
- child_theme/partials/ --> Ordner "shortcode" anlegen
- child_theme/partials/shortcode/ --> Datei "partialxyz.php" anlegen
- child_theme/partials/shortcode/partialxyz.php --> folgendes Snippet integrieren
<?php defined( 'ABSPATH' ) or die(); ?> <h1>It works! Partial wird angezeigt!</h1>
Schritt 4: Partial in Frontend ausgeben
Option A: Partial in WP-Admin einbinden
- beliebige Seite aufrufen
- WP-Block-Shortcode anlegen
- [theme_partial partial=partialxyz] eintragen, speichern und im Frontend ansehen
- wenn "It works! Partial wird angezeigt!" zu sehen ist, hat alles geklappt.
Option B: Partial in Template einbinden
<?php include 'partials/shortcode/newsletter_cta.php'; ?>
FAQ
Frage 1
TBD
Troubleshooting
Lösung 1
TBD
Nachfolgende(r) Prozess(e)
TBD via Verlinkung zu anderem(n) HowTo(s)
Quellen
- TBD