ThemeShaper Forums » Thematic

[closed]

Anybody have experience with custom permalinks?

(4 posts)
  • Started 3 years ago by Danieliser
  • Latest reply from papogator
  • This topic is not resolved
  1. Danieliser
    Member

    I Have posted this in several places but the wp forums take weeks sometimes to get responses.

    I am trying to get my custom taxonomies to operate like post categories, with permalinks like
    /post-type/tax-term/tax-term-child/tax-term-grandchild/post-name/

    ive got a function hooked into post_type_links that gives my links this structure. here it is

    add_filter('post_type_link', 'product_permalink', 10, 3);
    function product_permalink($permalink, $post_id, $leavename) {
    	if (strpos($permalink, '%res_brands%') === FALSE) return $permalink;
    
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'resbrands');
    		if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
    			$term_count = count($terms);
    			$i=1;
    			if($term_count > 1){
    				foreach( $terms as $term ) {
    					if (!empty($term) && is_object($term)) $taxonomy_slug .= $term->slug;
    					if($i < $term_count) $taxonomy_slug .= '/';
    					$i++;
    				}
    			} else {
    				$taxonomy_slug = $terms[0]->slug;
    			}
    		}
            //else $taxonomy_slug = 'not-rated';
    
    	return str_replace('%res_brands%', $taxonomy_slug, $permalink);
    }

    where my trouble lies is that the links lead to 404s due to the fact that %res_brands% could be parent/child/grandchild and it only recognizes 1 im assuiming, this is where i need help.

    im using

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag("%res_brands%", '([^/]+)', "resbrands=");

    and assuming that i could just change the ([^/]+) to something that only recognizes the last /grandchild or /child if no grandchild. does it work like a preg_replace? can i echo out multiple values into an array exploding at the /? any ideas would be helpful. Thanks

    Posted 3 years ago #
  2. Danieliser
    Member

    If anybody else wants parent/child category style links for custom post types and taxonomies this is what ive got working, probably not perfect but is working and not returning 404s.

    add_filter('post_link', 'res_brands_permalink', 10, 3);
    add_filter('post_type_link', 'res_brands_permalink', 10, 3);
    
    function res_brands_permalink($permalink, $post_id, $leavename) {
    	if (strpos($permalink, '%res_brands%') === FALSE) return $permalink;
    
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'resbrands', array('orderby' => 'count', 'order' => 'DESC'));
    		$count = count($terms);
    		if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
    			$taxonomy_slug = $terms[0]->slug;
    			return str_replace('%res_brands%', $taxonomy_slug, $permalink);
    		} else
    			return str_replace('/%res_brands%', '', $permalink);
    
    }
    
    add_filter('post_link', 'res_brands_child_permalink', 10, 3);
    add_filter('post_type_link', 'res_brands_child_permalink', 10, 3);
    
    function res_brands_child_permalink($permalink, $post_id, $leavename) {
    	if (strpos($permalink, '%res_brands_child%') === FALSE) return $permalink;
    
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'resbrands', array('orderby' => 'count', 'order' => 'ASC'));
    		$count = count($terms);
    		if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
    			$taxonomy_slug = $terms[0]->slug;
    			return str_replace('%res_brands_child%', $taxonomy_slug, $permalink);
    		} else
    			return str_replace('/%res_brands_child%', '', $permalink);
    
    }

    and my post type & rewrite rules

    register_post_type( 
    
    'residential',
    array(
         'public' => true,
         'rewrite' => true,
         'query_var' => 'residential'
         )
    );
    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag("%residential%", '([^/]+)', 'residential=');
    $wp_rewrite->add_rewrite_tag("%res_brands%", '([^/]+)', 'resbrands=');
    $wp_rewrite->add_rewrite_tag("%res_brands_child%", '([^/]+)', 'resbrands=');
    $wp_rewrite->add_permastruct('residential', "products/residential/%res_brands%/%res_brands_child%/%residential%/", $with_front = true);

    This gives links of /post-type/tax-term/tax-term-child/post-name

    I will be doing a full write up on my blog soon.

    Posted 3 years ago #
  3. Hey nice work. I had have similar code to do what you've descdribed. But you have an issue in your child function. Add this foreach and it should work fine:

    <i> In function res_brands_child_permalink($permalink, $post_id, $leavename) </i>


    foreach($terms as $term):
    if (!is_wp_error($terms) && !empty($terms) && is_object($term)) {
    $taxonomy_slug = $term->slug;
    $rval = str_replace('%product_cat_child%', $taxonomy_slug, $permalink);
    } else {
    $rval = str_replace('/%product_cat_child%', '', $permalink);
    }
    endforeach;

    Posted 2 years ago #
  4. One more thing, return $rval at end (just a personal preference)

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.