Last active
December 27, 2021 03:47
-
-
Save yuliyang/90ca2dfa7ae0547122a79950a5e9c307 to your computer and use it in GitHub Desktop.
[WordPress] Shortcodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'init', 'ching_shortcode' ); | |
function ching_shortcode() { | |
// Usage: [ching-shortcode text="Hello World!"] | |
add_shortcode( 'ching-shortcode', 'ching_test_shortcode' ); | |
function ching_test_shortcode( $atts ) { | |
$atts = shortcode_atts( array( | |
'text' => 'Hello World!', | |
'something' => 'So hungry.' | |
), $atts ); | |
return "text = {$atts['text']}"; | |
} | |
// Usage: [ching-wrap]content[/ching-wrap] | |
add_shortcode( 'ching-wrap', 'ching_wrap_shortcode' ); | |
function ching_wrap_shortcode( $atts, $content = "" ) { | |
return "content = $content"; | |
} | |
// Usage: [ching-get-template] | |
add_shortcode( 'ching-get-template', 'ching_gettemplate_shortcode' ); | |
function ching_gettemplate_shortcode( $atts ) { | |
$attributes = shortcode_atts( array( | |
'title' => false, | |
'limit' => 4, | |
), $atts ); | |
ob_start(); | |
// include template with the arguments (The $args parameter was added in v5.5.0) | |
get_template_part( 'template-parts/wpdocs-the-shortcode-template', null, $attributes ); | |
return ob_get_clean(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment