WordPress 分组输出不同 Custom Taxonomy 的 Custom Post Type 内容

给最近项目写的,不细说。

functions.php :

<?php
function show_cases($tax_cat,$tax) {
	$terms = get_terms(
		array(
				'taxonomy'   => $tax_cat,
				'hide_empty' => false,
		)
	);
	if ( ! empty( $terms ) && is_array( $terms ) ) {
		foreach ( $terms as $term ) { ?>
				<section class="cat" id="tax-<?php echo $term->slug; ?>" <?php $pd_cat_img = get_field('cat_img', $term); if ($pd_cat_img){ echo 'style="background-image:url(' . $pd_cat_img['url'] . ')"'; } ?>>
				<div class="container text-center">
				<div class="title text-left">
				<h3><?php echo $term->name; ?></h3>
				<p class="subtitle"><?php echo $term->slug; ?></p>
				<p class="desc"><?php echo $term->description; ?></p>
				</div>        

				<?php 
				$post_args = array(
					'posts_per_page' => 3,
					'post_type' => $tax,
					'tax_query' => array(
						array(
							'taxonomy' => $tax_cat,
							'field' => 'term_id',
							'terms' => $term->term_id,
						)
					)
				);
				$myposts = get_posts($post_args); 
				?>
				<div class="row">
					<?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
						<div class="col-md-4 hover-box">
							<div class="content">
								<a href="<?php the_permalink(); ?>">
									<div class="content-overlay"></div>
									<?php echo get_the_post_thumbnail( $post->ID, 'case-thumb', array( 'class' => 'content-image' ) ); ?>
									<div class="content-details fadeIn-bottom">
									<h4 class="content-title"><?php echo get_the_title($post->ID); ?></h4>
									<p class="content-text"><?php echo get_the_excerpt($post->ID); ?></p>
									</div>
								</a>
							</div>            
						</div>        
					<?php endforeach; // Term Post foreach ?>
				</div><br />
				<p class="text-center"><a class="btn btn-lg btn-primary" href="/<?php echo $tax_cat; ?>/<?php echo $term->slug; ?>">查看更多</a></p>		
				<?php wp_reset_postdata(); ?>

				</div></section><?php
		}
	}
}
?>

调用:

<?php
  if(is_page( 'case-photo' )){
		show_cases('foto_cat','foto');
	};     
?>
FavoriteLoadingAdd to favorites
欢迎给我打赏: