This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programmieren:wordpress:eigenen_shortcode_erstellen [2022/05/20 13:51] – jgehrke | programmieren:wordpress:eigenen_shortcode_erstellen [2022/12/17 12:28] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| Dieser Code ist ein Beispiel für einen eigene Shortcode, welcher einen Post-Teaser in einem Beitrag erzeugt. Der Redakteur kann optional eine Beitrags ID und die maximale Länge des Textauszuges (Excerpt) angeben. | Dieser Code ist ein Beispiel für einen eigene Shortcode, welcher einen Post-Teaser in einem Beitrag erzeugt. Der Redakteur kann optional eine Beitrags ID und die maximale Länge des Textauszuges (Excerpt) angeben. | ||
| + | |||
| + | ===== Einfaches Beispiel ===== | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace my_namespace; | ||
| + | |||
| + | /* | ||
| + | [my_shortcode post_id=" | ||
| + | [my_shortcode]$content[/ | ||
| + | */ | ||
| + | // Shortcode Registrieren | ||
| + | add_shortcode( ' | ||
| + | |||
| + | // Die Shortcode Funktion - hier nur Logik und HTML am besten in eine eigene Funktion legen | ||
| + | function shortcode_logic( $atts = [], $content = null ) { | ||
| + | $defaults = [ | ||
| + | ' | ||
| + | ]; | ||
| + | $atts | ||
| + | |||
| + | // Falls im Text-Content auch Shortcodes enthalten sein dürfen | ||
| + | // $content | ||
| + | $return_html = shortcode_html( $atts[' | ||
| + | |||
| + | return $return_html; | ||
| + | } | ||
| + | |||
| + | // HTML Renderer - zur sauberen Trennung | ||
| + | function shortcode_html( $post_id ){ | ||
| + | |||
| + | $return_html = " | ||
| + | <div class=\" | ||
| + | Die Post-ID ist: {$post_id} | ||
| + | </ | ||
| + | "; | ||
| + | |||
| + | return $return_html; | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Beispiel in Anwendung ===== | ||
| + | |||
| + | Dieser Shortcode gibt einen Post-Teaser aus. | ||
| <code php> | <code php> | ||