Add taxonomy combinations to YOAST sitemap

时间:2019-03-19 14:50:35

标签: php wordpress

Used rewrite rules to achieve the current url setup but I need these combinations also added to the sitemap. 'collection' is just a product attribute with the taxonomy of pa_collection

Current URL Setup

https://*****.com/collection/%COLLECTION-TERM%/product-tag/%PRODUCT-TAG-TERM%

<?php

/**
 *  Create a new custom yoast seo sitemap
 */
 
add_filter( 'wpseo_sitemap_index', 'ex_add_sitemap_custom_items' );
add_action( 'init', 'init_wpseo_do_sitemap_actions' );
// Add custom index
function ex_add_sitemap_custom_items(){
	global $wpseo_sitemaps;
	$date = $wpseo_sitemaps->get_last_modified('CUSTOM_POST_TYPE');
	$smp ='';
    	$smp .= '<sitemap>' . "\n";
	$smp .= '<loc>' . site_url() .'/CUSTOM_KEY-sitemap.xml</loc>' . "\n";
	$smp .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
	$smp .= '</sitemap>' . "\n";
	return $smp;
}
function init_wpseo_do_sitemap_actions(){
	add_action( "wpseo_do_sitemap_CUSTOM_KEY", 'ex_generate_origin_combo_sitemap');
}
function ex_generate_origin_combo_sitemap(){
	global $wpdb;
	global $wp_query;
	global $wpseo_sitemaps;
        $post_type = 'archive';
	wp_reset_query();
	$args = array(
		'posts_per_page'   => -1,
		'orderby'          => 'post_date',
		'order'            => 'DESC',
		'post_type'        => $post_type,
		'post_status'      => 'publish',
		'suppress_filters' => true
	);
	query_posts( $args );
	wp_reset_postdata();
	//echo '<pre>';print_r($url);echo '</pre>';
	$posts_array = get_posts( $args );
	$output = '';
	if( !empty( $posts_array ) ){
		$chf = 'weekly';
		$pri = 1.0;
		foreach ( $posts_array as $p ) {
			$p->post_type   = $post_type;
			$p->post_status = 'publish';
			$p->filter      = 'sample';
			$url = array();
			if ( isset( $p->post_modified_gmt ) && $p->post_modified_gmt != '0000-00-00 00:00:00' && $p->post_modified_gmt > $p->post_date_gmt ) {
				$url['mod'] = $p->post_modified_gmt;
			} else {
				if ( '0000-00-00 00:00:00' != $p->post_date_gmt ) {
					$url['mod'] = $p->post_date_gmt;
				} else {
					$url['mod'] = $p->post_date;
				}
			}
			$url['loc'] = site_url().'/sample/all/'.$p->post_name;
			$url['chf'] = $chf;
			$url['pri'] = $pri;
			$output .= $wpseo_sitemaps->sitemap_url( $url );
			// Clear the post_meta and the term cache for the post, as we no longer need it now.
			// wp_cache_delete( $p->ID, 'post_meta' );
			// clean_object_term_cache( $p->ID, $post_type );
		}
	}
	// Grab last modified date
	$sql  = $wpdb->prepare(" SELECT MAX(p.post_modified_gmt) AS lastmod
		FROM	$wpdb->posts AS p
		WHERE post_status IN ('publish') AND post_type = %s ", $post_type );
	$mod = $wpdb->get_var( $sql );
	// Generate terms URLs
	$practitioner_terms = get_terms( 'pa_collection', 'orderby=count&hide_empty=0' );
	$practitioner_termss = get_terms( 'product_tag', 'orderby=count&hide_empty=0' );

	if( !empty( $practitioner_terms ) ){
		$pri = 1;
		$chf = 'weekly';
		foreach ($practitioner_terms as $key => $term ){
			$url = array();
			$url['loc'] = site_url().'/collection/'.$term->slug.'/product-tag/';
		      	$url['pri'] = $pri;
		      	$url['mod'] = $mod;
		      	$url['chf'] = $chf;
		      	$output .= $wpseo_sitemaps->sitemap_url( $url );
		}
		foreach ($practitioner_termss as $key => $term ){
			$url = array();
			$url['loc'] = site_url().'/product-tag/'.$term->slug;
		      	$url['pri'] = $pri;
		      	$url['mod'] = $mod;
		      	$url['chf'] = $chf;
		      	$output .= $wpseo_sitemaps->sitemap_url( $url );
		}
	}

This is the section is where I need to combine the taxonomies. So far I have all the terms for both taxonomies on the same page but I need to have them combined to create all the possible combinations.

$practitioner_terms = get_terms( 'pa_collection', 'orderby=count&hide_empty=0' );
$practitioner_termss = get_terms( 'product_tag', 'orderby=count&hide_empty=0' ); 

if( !empty( $practitioner_terms ) ){
    $pri = 1;
    $chf = 'weekly';
    foreach ($practitioner_terms as $key => $term ){
        $url = array();
         $url['loc'] =site_url().'/collection/'.$term>slug.'/product-tag/';

            $url['pri'] = $pri;
            $url['mod'] = $mod;
            $url['chf'] = $chf;
            $output .= $wpseo_sitemaps->sitemap_url( $url );
    }
    foreach ($practitioner_termss as $key => $term ){
        $url = array();
        $url['loc'] = site_url().'/product-tag/'.$term->slug;
            $url['pri'] = $pri;
            $url['mod'] = $mod;
            $url['chf'] = $chf;
            $output .= $wpseo_sitemaps->sitemap_url( $url );
    }
}

1 个答案:

答案 0 :(得分:0)

想出一个可行的解决方案,可能对其他人有用...

$practitioner_terms = get_terms( 'pa_collection', 'orderby=count&hide_empty=0' );
$practitioner_termss = get_terms( 'product_tag', 'orderby=count&hide_empty=0' );

if( !empty( $practitioner_terms ) ){
    $pri = 1;
    $chf = 'weekly';
    foreach ($practitioner_terms as $key => $term ){
        foreach ($practitioner_termss as $key => $terms ){
        $url = array();
        $url['loc'] = site_url().'/collection/'.$term->slug.'/product-tag/'.$terms->slug;
            $url['pri'] = $pri;
            $url['mod'] = $mod;
            $url['chf'] = $chf;
            $output .= $wpseo_sitemaps->sitemap_url( $url );

    }

    }

}