WordPress.org

Forums

only home page after changing url rules for custom post type (1 post)

  1. adi_sandeep
    Member
    Posted 9 months ago #

    i have a custom post type named store, i needed to show brand name and then store name in url ex: http://topizza.prelaunchdomain.co.uk/pizza-hut/pizza-hut-lyon-chevreul/. i have the changed the url structure for this post type and it is working but when i open other pages like contact us the url is correct but the content is coming from home page

    add_action( 'init', 'tdd_add_rewrite_rules' );
    	add_filter( 'post_type_link', 'tdd_permalinks', 10, 3 );
    
    	function tdd_add_rewrite_rules() {
    
    		// Register custom rewrite rules
    
    		global $wp_rewrite;
    		$wp_rewrite->add_rewrite_tag( '%store%', '([^/]+)', 'store=' );
    		$wp_rewrite->add_rewrite_tag( '%stores_brand%', '([^/]+)', 'stores_brand=' ); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    		$wp_rewrite->add_permastruct( 'store', '/%stores_brand%/%store%', false ); 
    
    	}
    
    	function tdd_permalinks( $permalink, $post, $leavename ) {
    		global $wpdb;
    		$no_data = 'no-data';
    		$post_id = $post->ID;
    		if ( $post->post_type != 'store' || empty( $permalink ) || in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
    			return $permalink;
    		if ( $post->post_type == 'store'){
    		$var1 = get_post_meta( $post_id, '_wpcf_belongs_brand_id', true ); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    		$result = $wpdb->get_results( "select post_name from wp_posts where ID=" . $var1, ARRAY_A );
    		$var1 = sanitize_title( $result[0]['post_name'] ); 
    
    		if ( !$var1 )
    			$var1 = $no_data;
    		$permalink = str_replace( '%stores_brand%', $var1, $permalink ); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    //		$permalink = str_replace( '%city%', $var2, $permalink ); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    		return $permalink;
    	}
    	}

Reply

You must log in to post.

About this Topic