I am working on creating a shortcode function that will display a custom post type and is filterable by its category/taxonomy. I’ve figured out how to create the shortcode and have it display the post type. But I cannot get it to display the associated taxonomy categories.
I have created a custom post type called ‘testimonials’ I then created a taxonomy called ‘testimonial-category’. Inside of this taxonomy I have a category called ‘testimonial-home’.
I want to be able to use the shortcode with the ‘testimonial-home’ as its filter in order to display home page testimonials.
My shortcode works great without attributes: [list-testimonials] – this displays all testimonials. But when I add: [list-testimonials category=”testimonial-home”] nothing is displayed.
I’m stumped. I’m so close and I’m sure there is something very obvious that I am overlooking. Any and all help is so greatly appreciated! Thanks!
Here’s my shortcode function:
// create shortcode with parameters so that the user can define what’s queried – default is to list all blog posts
add_shortcode( 'list-testimonials', 'post_listing_parameters_shortcode' );
function post_listing_parameters_shortcode( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'type' => 'testimonials',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'category' => '',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'category_name' => $category,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div
class="small-12 medium-12 large-4 columns testimonial-column homepage- testimonial-column">
<!--HOME PAGE SINGLE TESTIMONIAL CONTAINER-->
<div
class="testimonial-container homepage-testimonial-container"
id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!--HOME PAGE SINGLE TESTIMONIAL TEXT-->
<?php if( get_field('testimonial_text') ): ?>
<div
class="testimonial testimonial-textarea">
<?php the_field('testimonial_text'); ?>
</div>
<?php endif ?>
<!--END HOME PAGE SINGLE TESTIMONIAL TEXT-->
<!--HOME PAGE SINGLE TESTIMONIAL DETAILS-->
<div
class="testimonial-details">
<!--HOME PAGE SINGLE TESTIMONIAL IMAGE-->
<?php if( get_field('testimonial_photo') ): ?>
<img
src="<?php the_field('testimonial_photo'); ?>"
class="testimonial-photo" />
<?php endif ?>
<!--HOME PAGE SINGLE TESTIMONIAL BIO INFO-->
<div
class="testimonial-bio">
<!--HOME PAGE SINGLE TESTIMONIAL NAME-->
<?php if( get_field('testimonial_name') ): ?>
<h5
class="testimonial-name">
<?php the_field('testimonial_name'); ?>
</h5>
<?php endif ?>
<!--END HOME PAGE SINGLE TESTIMONIAL NAME-->
<!--HOME PAGE SINGLE TESTIMONIAL TITLE-->
<?php if( get_field('testimonial_title') ): ?>
<p
class="testimonial-title">
<?php the_field('testimonial_title'); ?>
</p>
<?php endif ?>
<!--END HOME PAGE SINGLE TESTIMONIAL TITLE-->
<!--HOME PAGE SINGLE TESTIMONIAL COMPANY-->
<?php if( get_field('testimonial_company') ): ?>
<p
class="testimonial-company">
<?php the_field('testimonial_company'); ?>
</p>
<?php endif ?>
<!--END HOME PAGE SINGLE TESTIMONIAL COMPANY-->
</div>
<!--END HOME PAGE SINGLE TESTIMONIAL BIO INFO-->
</div>
<!--END HOME PAGE SINGLE TESTIMONIAL DETAILS-->
</div>
<!--END HOME PAGE SINGLE TESTIMONIAL CONTAINER-->
</div>
<!--HOME PAGE TESTIMONIALS COLUMN-->
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
I would start (if you haven’t already) with making sure the query works “in vivo”, that is, by itself on a page (not as a shortcode). Then iterate from there. Since custom taxonomies use a whole slew of different functions and key names than categories do, it’s important to test this first. Also, to be super overly pedantic: when referring to a custom taxonomy, the term for categories is term or terms.
For what it’s worth, a pristine custom post type loop example: