Skip to content

Instantly share code, notes, and snippets.

@yuriitaran
Last active April 19, 2018 11:00
Show Gist options
  • Save yuriitaran/07da57f3220cf52858ae055c7ca10aca to your computer and use it in GitHub Desktop.
Save yuriitaran/07da57f3220cf52858ae055c7ca10aca to your computer and use it in GitHub Desktop.
Sample shortcode with parameters
// More info: https://speckyboy.com/getting-started-with-wordpress-shortcodes-examples/
function sample_template($title, $type, $order) {
$the_query = new WP_Query( array( 'post_type' => $type, 'order' => $order, ) );
if ( $the_query->have_posts() ) : ?>
<h2><?php echo $title; ?></h2>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<p>
<?php echo esc_url( get_permalink( $post ) ); ?>
</p>
<?php endwhile;
wp_reset_postdata();
endif;
}
function sample_shortcode($atts) {
$title = $atts[title];
$type = $atts[type];
$order = $atts[order];
ob_start();
sample_template($title, $type, $order);
return ob_get_clean();
}
add_shortcode( 'sample_shortcode', 'sample_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment