如何将pa_collection与product_tag分类组合添加至Yoast站点地图?
Alright, let's get this sorted for you. You've already got code that pulls in both pa_collection terms and product tags, so the main task is to generate all their possible combinations and feed those URLs into Yoast SEO's sitemap. Here's how to do it:
Step 1: Core Logic Breakdown
We'll use Yoast SEO's wpseo_sitemap_urls filter to inject our custom combination URLs. The process is straightforward:
- Fetch all terms from the
pa_collectiontaxonomy - Fetch all terms from the
product_tagtaxonomy - Loop through every possible pair of collection + tag to generate the matching URL structure
- Add each valid URL to the sitemap array
Step 2: Full Code Implementation
Add this snippet to your theme's functions.php file (or a custom plugin, which is safer for theme updates):
/** * Add pa_collection + product_tag combination URLs to Yoast SEO sitemap */ add_filter( 'wpseo_sitemap_urls', 'add_collection_tag_combination_sitemap_urls' ); function add_collection_tag_combination_sitemap_urls( $urls ) { // Fetch all pa_collection terms (adjust hide_empty as needed) $collections = get_terms( array( 'taxonomy' => 'pa_collection', 'hide_empty' => false, // Set to true if you only want collections with products 'fields' => 'all' ) ); // Fetch all product_tag terms (adjust hide_empty as needed) $product_tags = get_terms( array( 'taxonomy' => 'product_tag', 'hide_empty' => false, // Set to true if you only want tags with products 'fields' => 'all' ) ); // Exit early if either taxonomy has no terms if ( empty( $collections ) || empty( $product_tags ) ) { return $urls; } // Get your site's base URL $base_url = home_url( '/' ); // Generate all collection + tag combinations foreach ( $collections as $collection ) { foreach ( $product_tags as $tag ) { // Build the full URL matching your rewrite structure $combination_url = trailingslashit( $base_url . 'collection/' . $collection->slug . '/product-tag/' . $tag->slug ); // Add the URL to the sitemap array $urls[] = array( 'loc' => $combination_url, 'lastmod' => current_time( 'mysql' ), // Use latest product update time here if possible 'changefreq' => 'weekly', // Adjust based on how often these pages change 'priority' => 0.6 // Set priority relative to other pages on your site ); } } return $urls; }
Key Notes & Adjustments
hide_emptyParameter: If you don't want to include collections/tags that have no products, sethide_emptytotruein theget_termscalls. This will reduce the number of URLs in your sitemap.- Performance Consideration: If you have hundreds of collections and tags, the total number of combinations could be very large. To optimize, you might want to only include combinations that actually have products. You'd add a check inside the nested loops using
wc_get_productsto verify if there are products matching both the collection and tag. - Accurate
lastmod: Instead of using the current time, you can query the most recent modified date of products in that collection+tag pair and use that forlastmod—this helps search engines know when the content was updated. - Testing: After adding the code, visit your sitemap index (e.g.,
yourdomain.com/sitemap_index.xml) and check if the custom URLs appear. You can also use Yoast's built-in sitemap validation tool to ensure everything is correctly formatted.
内容的提问来源于stack exchange,提问作者user2059376
相关产品推荐
相关产品推荐

