WordPress自定义分类法全术语页面模板创建求助
我来帮你搞定这个问题,你需要的是一个展示自定义分类法ctc_sermon_topic所有术语的导航页面,之前的模板文件名没选对——因为WordPress的模板层级里,taxonomy-ctc_sermon_topic.php是用来显示单个术语的归档页(比如你现在的archives/sermon-topic/contentment),而不是所有术语的列表页。下面给你两种可行的解决方案:
方法一:使用自定义页面模板(推荐,灵活可控)
这种方法需要你创建一个专门的页面模板,然后在后台新建页面并关联该模板,就能得到一个独立的导航页面。
- 在你的主题目录下新建一个名为
page-sermon-topics.php的文件,写入以下代码:
<?php /* Template Name: Sermon Topics Navigation */ get_header(); ?> <div class="content-area"> <main id="main" class="site-main" role="main"> <header class="page-header"> <h1 class="page-title"><?php esc_html_e( 'All Sermon Topics', 'your-theme-text-domain' ); ?></h1> </header> <?php // 获取所有sermon topic术语,可根据需求调整参数 $all_topics = get_terms( array( 'taxonomy' => 'ctc_sermon_topic', 'hide_empty' => false, // 设为true则只显示有文章的术语 'orderby' => 'name', 'order' => 'ASC', ) ); if ( ! empty( $all_topics ) && ! is_wp_error( $all_topics ) ) : ?> <ul class="sermon-topics-list"> <?php foreach ( $all_topics as $topic ) : ?> <li> <a href="<?php echo esc_url( get_term_link( $topic ) ); ?>"> <?php echo esc_html( $topic->name ); ?> <!-- 可选:显示该术语下的文章数量 --> <span class="topic-count">(<?php echo $topic->count; ?>)</span> </a> </li> <?php endforeach; ?> </ul> <?php else : ?> <p><?php esc_html_e( 'No sermon topics found.', 'your-theme-text-domain' ); ?></p> <?php endif; ?> </main> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
- 登录WordPress后台,新建一个页面(比如标题设为"Sermon Topics"),在页面编辑界面的「属性」面板里,选择刚才创建的「Sermon Topics Navigation」模板。
- 保存页面后,访问这个页面的URL,就能看到所有术语的导航列表,点击每个术语就能跳转到对应的归档页(比如
archives/sermon-topic/contentment)。
方法二:复用现有分类法模板,在根URL显示术语列表
如果你希望直接用archives/sermon-topic/这个根URL作为导航页面,不需要新建独立页面,可以修改现有的taxonomy-ctc_sermon_topic.php文件,添加判断逻辑:
<?php get_header(); ?> <div class="content-area"> <main id="main" class="site-main" role="main"> <?php // 判断是否是分类法的根页面(没有指定具体术语) if ( is_tax( 'ctc_sermon_topic' ) && ! get_queried_object() ) : ?> <header class="page-header"> <h1 class="page-title"><?php esc_html_e( 'All Sermon Topics', 'your-theme-text-domain' ); ?></h1> </header> <?php $all_topics = get_terms( array( 'taxonomy' => 'ctc_sermon_topic', 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', ) ); if ( ! empty( $all_topics ) && ! is_wp_error( $all_topics ) ) : ?> <ul class="sermon-topics-list"> <?php foreach ( $all_topics as $topic ) : ?> <li> <a href="<?php echo esc_url( get_term_link( $topic ) ); ?>"> <?php echo esc_html( $topic->name ); ?> <span class="topic-count">(<?php echo $topic->count; ?>)</span> </a> </li> <?php endforeach; ?> </ul> <?php else : ?> <p><?php esc_html_e( 'No sermon topics found.', 'your-theme-text-domain' ); ?></p> <?php endif; ?> <?php else : ?> <!-- 保留原来单个术语归档的代码,比如显示该术语下的文章列表 --> <header class="page-header"> <?php the_archive_title( '<h1 class="page-title">', '</h1>' ); ?> <?php the_archive_description( '<div class="taxonomy-description">', '</div>' ); ?> </header> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'template-parts/content', get_post_format() ); ?> <?php endwhile; ?> <?php the_posts_navigation(); ?> <?php else : ?> <?php get_template_part( 'template-parts/content', 'none' ); ?> <?php endif; ?> <?php endif; ?> </main> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
最后注意事项
- 替换代码中的
your-theme-text-domain为你主题实际使用的文本域。 - 修改完成后,进入后台「设置」→「固定链接」,随便选择一个选项保存(刷新WordPress的重写规则),确保新的URL能正常访问。
内容的提问来源于stack exchange,提问作者thetallweeks
相关产品推荐
相关产品推荐

