Skip to content

Instantly share code, notes, and snippets.

@Garconis
Last active August 31, 2017 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garconis/e8c54131e7237911449725860536acc0 to your computer and use it in GitHub Desktop.
Save Garconis/e8c54131e7237911449725860536acc0 to your computer and use it in GitHub Desktop.
WordPress | Create a shortcode that lists pages of a custom taxonomy
<?php
/* Usage:
[fs-local-type type="wordpress"]
You can add additional parameters to the shortcode to override settings
Note: "local-type" is the name of the Taxonomy, and "wordpress" is the name of one of the terms
*/
// create shortcode to list all Local Types
add_shortcode( 'fs-local-type', 'fs_local_type_shortcode' );
function fs_local_type_shortcode( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'type' => 'wordpress',
), $atts ) );
// define query parameters based on attributes above
$options = array(
'post_type' => $post_type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts_per_page,
'local-type' => $type,
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) {
echo'<ul class="local-listing">';
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
echo '<a href="'. get_permalink() .'">'. get_the_title() .'</a>';
echo '</li>';
endwhile;
wp_reset_postdata();
echo '</ul>';
$myvariable = ob_get_clean();
return $myvariable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment