Created
October 3, 2019 15:02
-
-
Save MrCoker/c93b50011d353fb7d80407051ee7cab8 to your computer and use it in GitHub Desktop.
Adding Template Parts with Shortcode
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 a shorcode to get the template part | |
function makeagency_shortcode() { | |
ob_start(); | |
get_template_part('templates/this', 'template'); | |
return ob_get_clean(); | |
} | |
add_shortcode( 'tax_calculator', 'makeagency_shortcode' ); | |
// Check the post for the shortcode and enque the scripts | |
function makeagency_shortcode_scripts() { | |
global $post; | |
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'makeagency_shortcode') ) { | |
wp_enqueue_style( 'shortcode_styles', get_template_directory_uri() . '/css/shortcode.css', array(), '1.0.0', 'all' ); | |
wp_enqueue_script( 'shortcode_js', get_template_directory_uri() . '/js/shortcode.js', array(), '1.0.0', 'all' ); | |
} | |
} | |
add_action( 'wp_enqueue_scripts', 'makeagency_shortcode_scripts'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment