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

如何给PHP的switch case添加多匹配项?WP模板加载问题咨询

Hey there! Let's break down your two questions step by step—totally get being a PHP newbie, we’ve all been there. 😊

1. Adding Multiple Matching Values to a PHP Switch Case

There are two clean, practical ways to handle multiple matches in a switch statement:

Method 1: Stack Case Statements (No Break Until the End)

You can list multiple case lines back-to-back without a break; they’ll all trigger the same code block until a break is encountered. Perfect for grouping similar values:

$item = 'banana';

switch($item) {
    case 'apple':
    case 'banana':
    case 'cherry':
        echo "This is a delicious fruit!";
        break; // Only break after the last matching case
    case 'carrot':
    case 'spinach':
        echo "This is a crunchy vegetable!";
        break;
    default:
        echo "Hmm, not sure what this is!";
}

Method 2: Use in_array() with a true Switch Condition

If you’re dealing with larger groups of values, this approach is more compact. Set the switch condition to true, then check against arrays in each case:

$item = 'spinach';

switch(true) {
    case in_array($item, ['apple', 'banana', 'cherry']):
        echo "Fruit mode activated!";
        break;
    case in_array($item, ['carrot', 'spinach', 'broccoli']):
        echo "Vegetable mode activated!";
        break;
    default:
        echo "Unknown item type!";
}
2. Fixing Your WordPress Query & Loading Dynamic Post Templates

First, let’s fix a critical error in your $query_args—you can’t list post types like 'feature', 'fashion', 'film' directly; the post_type parameter needs an array if targeting multiple types. Here’s the corrected query, plus how to load different templates for a varied homepage layout:

Step 1: Corrected WP_Query Code

<?php
// Ensure pagination works correctly
$paged = get_query_var('paged') ? get_query_var('paged') : 1;

$query_args = array(
    'post_type' => ['post', 'feature', 'fashion', 'film'], // Multiple post types as an array
    'posts_per_page' => 100,
    'paged' => $paged,
    'cat' => -13 // Exclude category with ID 13
);

$query = new WP_Query($query_args);

if ($query->have_posts()) {
    $post_count = 0; // Counter for layout variations
    
    while ($query->have_posts()) {
        $query->the_post();
        $post_count++;
        
        // Option 1: Load template based on post type
        // Looks for files like content-post.php, content-feature.php in your theme
        get_template_part('template-parts/post/content', get_post_type());
        
        // Option 2: Load different templates by position (e.g., first post is large)
        // if ($post_count === 1) {
        //     get_template_part('template-parts/post/content', 'large');
        // } else {
        //     get_template_part('template-parts/post/content', 'small');
        // }
        
        // Option 3: Load template based on post category
        // $primary_cat = get_the_category()[0];
        // get_template_part('template-parts/post/content', $primary_cat->slug);
    }

    // Add pagination links
    the_posts_pagination(array(
        'mid_size' => 2,
        'prev_text' => __('Previous', 'your-theme-domain'),
        'next_text' => __('Next', 'your-theme-domain'),
    ));

    // Reset post data to avoid breaking WordPress global loop
    wp_reset_postdata();
} else {
    // Show "no posts found" template
    get_template_part('template-parts/content', 'none');
}
?>

Key Tips for Dynamic Layouts:

  • get_template_part() is your go-to: This WordPress function lets you split your theme into reusable files. For example, get_template_part('template-parts/post/content', 'large') loads content-large.php inside template-parts/post/ in your theme folder.
  • Never skip wp_reset_postdata(): This ensures your custom query doesn’t interfere with WordPress’s main global loop, preventing weird bugs later.
  • Customize the condition: Pick whatever fits your layout goal—post type, position in the loop, category, or even custom fields (use get_post_meta() to check custom field values).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:48:53