Last active
April 17, 2024 15:46
-
-
Save MadCowWeb/c10820f2edcb0bc4cd8937df1564ea04 to your computer and use it in GitHub Desktop.
Create Shortcodes in WordPress
This file contains 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
add_action('init', 'madcowweb_shortcodes'); | |
function madcowweb_shortcodes() { | |
add_shortcode('show-cows', 'show_cows'); | |
} | |
//SIMPLE TEXT | |
function show_cows() { | |
$html = '<h1>I love cows</h1>'; | |
return $html; | |
} | |
// DISPLAY CUSTOM POSTS | |
function show_cows() { | |
$html = '<div class="cows-container">'; | |
$cows = get_posts(array( | |
'posts_per_page' => -1, | |
'post_type' => 'cows', | |
)); | |
foreach ($cows as $cow) : | |
$cow_name = get_the_title($cow->ID); | |
$cow_image = get_the_post_thumbnail($cow->ID); | |
$cow_content = get_field('cow_description', $cow->ID); | |
$cow_talent = get_field('special_talent', $cow->ID); | |
$html .= '<div class="cow">'; | |
$html .= $cow_image; | |
$html .= '<h3>' . $cow_name . '</h3>'; | |
$html .= $cow_content; | |
$html .= '<h5>Special Talent: <span>' . $cow_talent . '</span></h5>'; | |
$html .= '</div><!-- end cow -->'; | |
endforeach; | |
$html .= '</div><!-- end cows-container -->'; | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment