Shortcode to Return Custom Post Type by Category
Jul 2, 2020 | WordPress
function x_shortcode($atts){
ob_start();
$i = 0;
$var = shortcode_atts( array('category' => 'x'), $atts );
$args = array(
'post_type' => 'staff-member',
'posts_per_page' => '-1',
'publish_status' => 'published',
'category_name' => $var['category']
);
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()){
$query->the_post();
$x = get_field( "x" );
$xx = get_field( "xx" );
$xxx = get_the_post_thumbnail( $post_id, 'full' );
if($i % 3 == 0){ ?>
<div class="x-row">
<?php } ?>
<div class="staff-card">
<?php echo $xxx; ?>
<div class="staff-inner">
<h3 class="x"><?php the_title(); ?></h3>
<p class="xx"><?php echo $x; ?></p>
<p class="xxx"><?php echo $xx; ?></p>
</div>
</div>
<?php
$i++;
if($i % 3 == 0){ ?>
</div>
<?php }
}
}
?>
</div>
<?php
return ob_get_clean();
}
add_shortcode( 'shortcode-x', 'x_shortcode' );
Shortcode that displays custom post type. Takes one parameter for category.