我正在使用插件CPT UI。我想显示类别名称,并在该类别下显示该帖子。我希望它看起来像这样。enter image description here
我想对每个类别重复此操作,因此,每添加一个新类别,它就会在页面上显示类别名称和相关文章。到目前为止,我已经尝试过
<div id="row-portfolio" class="row">
<div class="col-md-12">
<h2>Achtertuin</h2>
</div>
</div>
<?php
$loop = new WP_Query( array(
'post_type' => 'projecten',
'cat' => '27', // Whatever the category ID is for your aerial category
'posts_per_page' => 10,
'orderby' => 'date', // Purely optional - just for some ordering
'order' => 'DESC' // Ditto
) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<!--single block-->
<a href="<?php the_permalink();?>" rel="bookmark">
<div class="col-md-6 col-sm-6 col-xs-12 float-left">
<div id="card" class="card">
<?= get_the_post_thumbnail();?>
<div id="card-body" class=" card-body ">
<h5 class="card-title "><?php the_title(); ?></h5>
<hr class="hrline">
<p class="card-text ">
<?php the_field('secundaire_titel'); ?>
</p>
</div>
</div>
<div>
</a>
<!--single block-->
<?php endwhile; ?>
有人可以帮我吗?
您需要遍历所有术语(类别)并在每个术语中设置查询:
<?php
// Get the array of all the term objects in your taxonomy.
$cats = get_terms( 'project_categorieen' );
// Loop through all the terms.
foreach ( $cats as $cat ) :
?>
<div id="row-portfolio" class="row">
<div class="col-md-12">
<h2><?php echo $cat->name; // This gets your category name ?></h2>
</div>
</div>
<?php
$cat_loop = new WP_Query( [
'post_type' => 'projecten',
// Need to use tax_query since it is a custom taxonomy. It's an array of arrays.
'tax_query' => [
[
'taxonomy' => 'project_categorieen',
// Get the term id from term object.
'terms' => $cat->term_id,
],
],
// Get all posts for each term/category
'posts_per_page' => - 1,
] );
while ( $cat_loop->have_posts() ) : $cat_loop->the_post(); ?>
<!--single block-->
<a href="<?php the_permalink(); ?>" rel="bookmark">
<div class="col-md-6 col-sm-6 col-xs-12 float-left">
<div id="card" class="card">
<?php get_the_post_thumbnail(); ?>
<div id="card-body" class=" card-body ">
<h5 class="card-title "><?php the_title(); ?></h5>
<hr class="hrline">
<p class="card-text ">
<?php the_field( 'secundaire_titel' ); ?>
</p>
</div>
</div>
</div>
</a>
<!--single block-->
<?php endwhile; ?>
<?php wp_reset_query(); // reset the query to start over.
?>
<?php endforeach; // End the looping of the terms/categories ?>