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

自定义短代码调用Stories自定义文章类型Fostering分类失败

Fixing Your Stories Shortcode Issue

Let's break down the problems with your current code and fix them one by one:

1. Mismatched Shortcode Parameter

You're calling the shortcode with [stories category="fostering"], but your function is looking for an attribute named cat (from $atts = shortcode_atts( array( 'cat' => '' ), $atts );). These need to match. Either adjust the shortcode to use cat="fostering", or update the attribute key in your function to category (we'll go with the latter to match your existing shortcode call).

2. Incorrect Taxonomy in tax_query

When you created custom categories for your stories post type, you almost certainly registered a custom taxonomy (not WordPress's default category taxonomy, which is meant for regular posts). You'll need to replace 'taxonomy' => 'category' with the actual slug of your custom taxonomy. For example, if you registered it as story_categories (common naming convention), use that slug here.

3. Direct echo in Shortcode Function

WordPress shortcodes require you to return content instead of echoing it directly. Echoing will cause your stories to output in the wrong part of the page (often before the main content loads). Instead, build your output into a variable and return it at the end.

Corrected Code

Here's the fixed version of your function, addressing all three issues:

function register_shortcodes() { 
    add_shortcode( 'stories', 'stories_function' ); 
} 
add_action( 'init', 'register_shortcodes' ); 

function stories_function($atts) { 
    global $post; 
    // Match the attribute name to your shortcode call
    $atts = shortcode_atts( array( 
        'category' => '' // Updated to match your [stories category="fostering"] call
    ), $atts ); 

    // Replace 'story_categories' with your actual custom taxonomy slug
    $loop = new WP_Query( array( 
        'posts_per_page' => 3, 
        'post_type' => 'stories', 
        'orderby' => 'rand', 
        'order' => 'ASC', 
        'tax_query' => array( 
            array( 
                'taxonomy' => 'story_categories', // Update this to your taxonomy's slug
                'field' => 'slug', 
                'terms' => sanitize_title( $atts['category'] )
            ) 
        ) 
    ) ); 

    if( ! $loop->have_posts() ) { 
        return '<p>No stories found in this category.</p>'; // Return a user-friendly message instead of false
    } 

    // Build output in a variable instead of echoing directly
    $output = '';
    while( $loop->have_posts() ) { 
        $loop->the_post(); 
        $output .= '<h3>' . get_the_title() . '</h3>';
        $output .= '<div class="story-content">' . get_the_content() . '</div>';
    } 
    wp_reset_postdata(); 

    return $output; // Return the final compiled content
}

Quick Additional Checks

  • Confirm your custom taxonomy slug: Go to your WordPress admin, navigate to the categories page for your stories post type, check the URL – the taxonomy slug will be in the path (e.g., edit-tags.php?taxonomy=story_categories).
  • Verify the category slug: Make sure fostering is the correct slug for your category (check the "Slug" field when editing the category in the admin).
  • Clear any caching plugins you're using, as cached pages can hide code changes until cleared.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:55:58