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

如何创建仅显示同时属于两个分类文章的WordPress归档页

Got it, let's tackle this problem directly—you want an archive page that shows posts only in both of two specific categories, no page builders like Visual Composer or Divi needed. Here are a couple of solid, code-based approaches to implement this right in your WordPress theme files:

Method 1: Custom Page Template (Full Control)

This gives you total control over the layout while matching your theme's existing design. Here's how to set it up:

  1. In your active theme folder, make a copy of page.php and rename it page-dual-category.php.

  2. At the very top of the new file, add this template declaration so WordPress recognizes it:

    <?php /* Template Name: Dual Category Archive */ ?>
    
  3. Find the main loop section (usually starts with if ( have_posts() ) : while ( have_posts() ) : the_post();). Replace that entire loop block with this custom query:

    <?php
    // Replace these with your category slugs (or use 'id' instead of 'slug' below if you prefer IDs)
    $cat_slug_1 = 'tech';
    $cat_slug_2 = 'reviews';
    
    // Build the query for posts in BOTH categories
    $dual_cat_args = array(
        'post_type' => 'post',
        'posts_per_page' => get_option('posts_per_page'), // Matches your site's default posts per page
        'paged' => get_query_var('paged') ? get_query_var('paged') : 1, // Handles pagination
        'tax_query' => array(
            'relation' => 'AND', // Critical: ensures posts are in BOTH categories
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $cat_slug_1
            ),
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $cat_slug_2
            )
        )
    );
    
    $dual_cat_query = new WP_Query($dual_cat_args);
    
    if ($dual_cat_query->have_posts()) :
        while ($dual_cat_query->have_posts()) : $dual_cat_query->the_post();
            // Use your theme's existing content template to keep styling consistent
            get_template_part('template-parts/content', get_post_format());
        endwhile;
    
        // Add pagination (use your theme's built-in pagination function if available)
        the_posts_pagination(array(
            'total' => $dual_cat_query->max_num_pages,
            'prev_text' => __('← Previous', 'your-theme-slug'),
            'next_text' => __('Next →', 'your-theme-slug')
        ));
    
        wp_reset_postdata(); // Reset the query to avoid conflicts with other content
    else :
        // Show your theme's "no posts found" message
        get_template_part('template-parts/content', 'none');
    endif;
    ?>
    
  4. Save the file, then go to WordPress admin and create a new page. Under "Page Attributes", select the "Dual Category Archive" template from the dropdown. Publish the page, and it will now display posts that are in both of your target categories.

Method 2: Modify the Main Query with pre_get_posts (Cleaner for Existing Pages)

If you prefer using a standard page template instead of creating a custom one, you can hook into WordPress's query system to adjust the main query for a specific page:

  1. Open your theme's functions.php file (or use a child theme to avoid losing changes on updates).

  2. Add this code snippet:

    function filter_dual_category_archive($query) {
        // Target only the main query on your specific archive page (replace 'dual-category-archive' with your page's slug)
        if (is_page('dual-category-archive') && $query->is_main_query() && !is_admin()) {
            $query->set('tax_query', array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug', // Swap to 'id' if using category IDs
                    'terms' => 'tech'
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => 'reviews'
                )
            ));
        }
    }
    add_action('pre_get_posts', 'filter_dual_category_archive');
    
  3. Replace 'dual-category-archive' with the slug of the page you want to use as your archive, and update the category slugs/IDs to match your target categories.

  4. Save the file, and your chosen page will now automatically display posts that belong to both categories.

Pro Tips:

  • Use Category IDs if Needed: If you're worried about slugs changing, swap 'field' => 'slug' to 'field' => 'id' and replace the terms with numeric category IDs (find these in WordPress admin under Categories > Edit).
  • Test Pagination: Make sure to test pagination links to ensure they correctly load subsequent pages of posts.
  • Keep Styling Consistent: Using get_template_part in Method 1 ensures your post layout matches the rest of your theme, so you don't have to rewrite styling code.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:37:34