You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为博客元信息添加自定义分类法:Posted In搜索无内容求助

Fixing Missing "Posted In" Content for Custom Taxonomies in WordPress

Hey Ashley, let's get that missing custom taxonomy content showing up under "Posted In"—it's a common fix, and I'll walk you through it step by step.

First, the core issue: your categories.php (or whichever template handles post metadata) is probably only calling WordPress's default category function (the_category()) instead of fetching your custom taxonomy terms. Here's how to add the custom taxonomy with proper if/else checks to handle empty cases:

Step 1: Locate the "Posted In" section in your template

Open your theme's categories.php (or if you're viewing single posts, check single.php or content.php—look for the line with "Posted In").

Step 2: Replace with custom taxonomy code

Swap out the existing "Posted In" code with this snippet, making sure to replace your_custom_taxonomy_slug with the actual slug you used when registering your custom taxonomy:

<?php
// Fetch terms from your custom taxonomy
$custom_tax_terms = get_the_terms( get_the_ID(), 'your_custom_taxonomy_slug' );

// Check if terms exist and no errors occurred
if ( $custom_tax_terms && ! is_wp_error( $custom_tax_terms ) ) :
    // Build links for each term
    $term_links = [];
    foreach ( $custom_tax_terms as $term ) {
        $term_links[] = '<a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a>';
    }
    // Join terms into a comma-separated list
    $term_list = implode( ', ', $term_links );
?>
<p class="posted-in">Posted In: <?php echo $term_list; ?></p>
<?php else : ?>
    <!-- Fallback if no terms are assigned -->
    <p class="posted-in">Posted In: Uncategorized</p>
<?php endif; ?>

Step 3: (Optional) Show both default categories and custom taxonomy

If you want to display both WordPress's default categories AND your custom taxonomy terms together, use this combined snippet:

<?php
// Get default categories
$default_categories = get_the_category();
// Get custom taxonomy terms
$custom_tax_terms = get_the_terms( get_the_ID(), 'your_custom_taxonomy_slug' );

$all_terms = [];

// Add default category links
if ( $default_categories && ! is_wp_error( $default_categories ) ) {
    foreach ( $default_categories as $cat ) {
        $all_terms[] = '<a href="' . esc_url( get_category_link( $cat ) ) . '">' . esc_html( $cat->name ) . '</a>';
    }
}

// Add custom taxonomy links
if ( $custom_tax_terms && ! is_wp_error( $custom_tax_terms ) ) {
    foreach ( $custom_tax_terms as $term ) {
        $all_terms[] = '<a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a>';
    }
}

// Display combined terms or fallback
if ( ! empty( $all_terms ) ) :
    $term_list = implode( ', ', $all_terms );
?>
<p class="posted-in">Posted In: <?php echo $term_list; ?></p>
<?php else : ?>
<p class="posted-in">Posted In: Uncategorized</p>
<?php endif; ?>

Quick Checks to Avoid Headaches

  • Double-check your custom taxonomy's registration code: make sure the public parameter is set to true, and show_ui is also true—this ensures terms are visible and accessible via template functions.
  • Clear your site cache (if you're using a caching plugin) after making template changes—sometimes old content sticks around.
  • Always edit your child theme's files instead of the parent theme—this way your changes won't get overwritten when the theme updates.

Let me know if you hit any snags with this—happy to help adjust the code to fit your specific setup!

内容的提问来源于stack exchange,提问作者Ashley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:29:25