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

WordPress自定义文章类型短代码开发:自定义字段无法显示求助

Fixing Custom Field Display in Your WordPress Custom Post Type Shortcode

Hey Ella, great job getting the basic shortcode working to pull the post ID and title! Let's fix that custom field issue—whether you're using native WordPress custom fields or Advanced Custom Fields (ACF) repeaters, we can get those showing up properly.

Here's a revised version of your shortcode that handles both regular custom fields and ACF repeater fields, with explanations of the key bits:

function feature_diagram_shortcode($atts) {
    // Set default shortcode parameters
    $atts = shortcode_atts(array(
        'category_name' => '',
    ), $atts);

    // Exit early if no category is specified
    if(empty($atts['category_name'])) {
        return '<p>Please specify a category using the category_name parameter.</p>';
    }

    // Query your custom post type (replace 'feature_diagram' with your actual CPT slug)
    $args = array(
        'post_type' => 'feature_diagram',
        'posts_per_page' => 1,
        'category_name' => $atts['category_name'],
        'post_status' => 'publish'
    );

    $query = new WP_Query($args);
    $output = '';

    if($query->have_posts()) {
        while($query->have_posts()) {
            $query->the_post();

            // Your existing ID/title output
            $output .= '<div class="feature-diagram-item">';
            $output .= '<h3>Post ID: ' . get_the_ID() . ' - ' . get_the_title() . '</h3>';

            // --- 1. Display a regular custom field ---
            // Option A: Native WordPress custom field (uses get_post_meta)
            $feature_desc = get_post_meta(get_the_ID(), 'feature_description', true);
            if(!empty($feature_desc)) {
                $output .= '<p class="feature-description">' . esc_html($feature_desc) . '</p>';
            }

            // Option B: ACF field (simpler with get_field)
            // $feature_desc_acf = get_field('feature_description');
            // if(!empty($feature_desc_acf)) {
            //     $output .= '<p class="feature-description">' . esc_html($feature_desc_acf) . '</p>';
            // }

            // --- 2. Display an ACF Repeater field ---
            if(have_rows('repeaters')) { // Replace 'repeaters' with your repeater field name
                $output .= '<div class="feature-repeaters">';
                $output .= '<h4>Repeater Field Content:</h4>';
                $output .= '<ul>';
                
                while(have_rows('repeaters')) {
                    the_row();
                    // Pull sub-fields (replace with your actual sub-field names)
                    $sub_title = get_sub_field('sub_title');
                    $sub_content = get_sub_field('sub_content');
                    
                    $output .= '<li>';
                    $output .= '<strong>' . esc_html($sub_title) . '</strong>';
                    $output .= '<p>' . esc_html($sub_content) . '</p>';
                    $output .= '</li>';
                }
                
                $output .= '</ul>';
                $output .= '</div>';
            }

            $output .= '</div>';
        }
        // Critical: Reset post data to avoid breaking global WordPress loops
        wp_reset_postdata();
    } else {
        $output .= '<p>No posts found for the specified category.</p>';
    }

    return $output;
}
add_shortcode('feature-diagram', 'feature_diagram_shortcode');

Key Notes to Make This Work for You:

  • Replace the CPT slug: Swap 'feature_diagram' with the actual slug of your custom post type.
  • Custom field names: Double-check that feature_description and repeaters match the exact field keys you set up (case-sensitive!).
  • ACF vs Native Fields: Use the appropriate code block for your setup—native fields use get_post_meta, while ACF fields work better with get_field and have_rows.
  • Security: Always use esc_html() when outputting user-generated content to prevent XSS vulnerabilities.
  • Reset Post Data: Don't skip wp_reset_postdata()—it ensures your shortcode doesn't interfere with other parts of your site's content loop.

Quick Troubleshooting Tips:

  1. If fields still don't show up, verify the post actually has content entered for those custom fields.
  2. For ACF, confirm the field group is assigned to your custom post type in the ACF field settings.
  3. Use var_dump(get_post_meta(get_the_ID(), 'feature_description', true)) temporarily to check if the field data is being pulled correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:57:09