Skip to content

Instantly share code, notes, and snippets.

@corilam
Last active January 15, 2023 06:33
Show Gist options
  • Save corilam/d6c28ef33e7b86fca695f5ade9ba7233 to your computer and use it in GitHub Desktop.
Save corilam/d6c28ef33e7b86fca695f5ade9ba7233 to your computer and use it in GitHub Desktop.
WP Shortcode with multi properties
function my_testimonial_accordion($atts){
ob_start();
$newcat = shortcode_atts(array(
'testcat' => '',
), $atts );
if ( $newcat['testcat'] ) {
// Parse type into an array. Whitespace will be stripped.
$newcat['testcat'] = array_map( 'trim', str_getcsv( $newcat['testcat'], ',' ) );
}
$output = $newcat['testcat'];
$testargs = array(
'post_type' => 'testimonial', // Change CPT
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'type', // Change to custom tax
'field' => 'slug',
'terms' => $output,
),
),
);
$testloop = new WP_Query( $testargs );
?>
<div class="grid">
<?php if ( $testloop->have_posts() ) :
while ( $testloop->have_posts() ) : $testloop->the_post(); ?>
<div class="testimonial-tiles" style="width:100%;text-align:left;">
<h5><?php the_title(); ?></h5>
<p><?php the_content(); ?></p>
</div>
<?php
endwhile;
endif; ?>
</div>
<?php
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('my_testimonial','my_testimonial_accordion');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment