
本教程详细阐述了如何在wordpress中,当按自定义分类法(如作品集类别)过滤或展示自定义文章类型时,同时显示该分类法的描述。核心方法是首先遍历所有分类法术语,然后针对每个术语执行一个独立的wp_query来获取相关文章,从而在显示文章列表的同时,展示对应分类的详细描述信息。
在WordPress开发中,我们经常会创建自定义文章类型(Custom Post Types, CPT)并为其关联自定义分类法(Custom Taxonomies)。一个常见的需求是,当用户浏览或过滤特定分类下的文章时,不仅要显示该分类下的文章列表,还需要同时展示该分类法的描述信息,以提供更丰富的上下文。本文将详细介绍如何通过遍历分类法术语并结合WP_Query来实现这一功能。
要实现为每个分类显示其描述以及该分类下的文章,最直接且有效的方法是采用“术语驱动的查询”策略。这意味着我们将不再仅仅查询文章,而是首先获取所有目标分类法中的术语(term),然后针对每一个术语,执行一个独立的WordPress查询(WP_Query)来获取与其关联的自定义文章。这种方法确保了在显示文章之前,我们已经拥有了当前术语的所有详细信息,包括其描述。
以下是实现此功能的具体步骤:
首先,我们需要使用 get_terms() 函数来获取特定自定义分类法下的所有术语。这个函数返回一个包含所有术语对象的数组,每个对象都包含了术语的名称、描述、slug等信息。
<?php
// 假设你的自定义分类法是 'portfolio_category'
$taxonomy_slug = 'portfolio_category';
$terms = get_terms($taxonomy_slug, array(
'hide_empty' => true // 只获取有文章关联的术语
));
// 检查是否成功获取到术语且没有WordPress错误
if (!is_wp_error($terms) && !empty($terms)) :
// 后续步骤将在循环内部进行
else :
echo '<p>未找到任何分类术语或发生错误。</p>';
endif;
?>获取到术语列表后,我们需要遍历这些术语。在每次循环中,我们将:
<?php
// ... (接上一步的代码)
if (!is_wp_error($terms) && !empty($terms)) :
foreach ($terms as $term) :
// 为每个术语创建一个容器,例如一个行或一个区块
?>
<div class="category-section">
<div class="category-header">
<h2><?php echo esc_html($term->name); ?></h2>
<?php if (!empty($term->description)) : ?>
<p class="category-description"><?php echo esc_html($term->description); ?></p>
<?php endif; ?>
</div>
<div class="posts-grid row">
<?php
// 构建WP_Query参数,查询当前术语下的自定义文章
$args = [
'post_type' => 'your_custom_post_type', // 替换为你的自定义文章类型 slug
'post_status' => 'publish',
'posts_per_page' => -1, // 显示所有文章
'tax_query' => [
[
'taxonomy' => $taxonomy_slug,
'field' => 'term_id',
'terms' => $term->term_id,
],
],
];
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
// 显示每篇文章的标题、链接等信息
?>
<div class="col-lg-4 col-md-6 mb-4">
<div class="post-item">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<!-- 可以添加文章缩略图、摘要等 -->
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail', array('class' => 'img-fluid')); ?>
</a>
<?php endif; ?>
<p><?php the_excerpt(); ?></p>
</div>
</div>
<?php
endwhile;
wp_reset_postdata(); // 重置文章数据
else :
echo '<div class="col-12"><p>此分类下暂无文章。</p></div>';
endif;
?>
</div><!-- .posts-grid -->
</div><!-- .category-section -->
<?php
endforeach;
endif;
?>将以上步骤整合,一个完整的实现代码示例如下。请根据你的实际情况替换 your_custom_post_type 和 portfolio_category。
<?php
/**
* 在WordPress中按自定义分类法分组显示文章,并展示分类法描述。
*/
// 定义你的自定义文章类型和自定义分类法
$custom_post_type = 'your_custom_post_type'; // 例如: 'portfolio'
$taxonomy_slug = 'portfolio_category'; // 例如: 'portfolio_category'
// 获取指定分类法下的所有术语
$terms = get_terms($taxonomy_slug, array(
'hide_empty' => true, // 只显示包含文章的术语
'orderby' => 'name',
'order' => 'ASC',
));
// 检查是否成功获取到术语且没有WordPress错误
if (!is_wp_error($terms) && !empty($terms)) :
?>
<div class="custom-taxonomy-listing">
<?php
foreach ($terms as $term) :
// 为每个分类术语创建一个独立的展示区块
?>
<section class="category-group mb-5">
<header class="category-group-header mb-4">
<h2 class="category-name display-4"><?php echo esc_html($term->name); ?></h2>
<?php if (!empty($term->description)) : ?>
<p class="category-description lead"><?php echo esc_html($term->description); ?></p>
<?php endif; ?>
</header>
<div class="row posts-in-category">
<?php
// 构建WP_Query参数,查询当前术语下的自定义文章
$args = [
'post_type' => $custom_post_type,
'post_status' => 'publish',
'posts_per_page' => -1, // 显示所有文章
'tax_query' => [
[
'taxonomy' => $taxonomy_slug,
'field' => 'term_id',
'terms' => $term->term_id,
],
],
'orderby' => 'date', // 按日期排序
'order' => 'DESC', // 降序
];
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<div class="col-lg-4 col-md-6 mb-4">
<article id="post-<?php the_ID(); ?>" <?php post_class('post-card'); ?>>
<?php if (has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium', array('class' => 'img-fluid rounded')); ?>
</a>
</div>
<?php endif; ?>
<div class="post-content p-3">
<h3 class="post-title h5 mt-2"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>" class="read-more btn btn-sm btn-primary mt-2">阅读更多</a>
</div>
</article>
</div>
<?php
endwhile;
wp_reset_postdata(); // 重置文章数据,非常重要
else :
// 如果当前分类下没有文章
?>
<div class="col-12">
<p class="text-muted">此分类“<?php echo esc_html($term->name); ?>”下暂无相关文章。</p>
</div>
<?php
endif;
?>
</div><!-- .row.posts-in-category -->
</section><!-- .category-group -->
<?php
endforeach;
?>
</div><!-- .custom-taxonomy-listing -->
<?php
else :
// 如果没有获取到任何分类术语
?>
<p class="alert alert-warning">抱歉,未找到任何自定义分类术语或发生错误。</p>
<?php
endif;
?>通过上述方法,我们可以有效地在WordPress中为自定义文章类型实现按分类法分组显示,并同时展示每个分类法的描述信息。这种结构化的展示方式不仅提升了内容的可读性,也为访问者提供了更全面的信息,是构建专业WordPress网站的常用技巧。遵循本文的指导和最佳实践,你将能够轻松地将此功能集成到你的项目中。
以上就是在WordPress中为自定义文章类型显示分类法描述的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号