Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. // Custom Taxonomy Permalinks -- This will use the Taxonomy Variable as part of the URL dynamically
  2. // You will need to add the %variable% from line 10 in your post type like this
  3. // 'rewrite' => array('slug' => '/%variable%', 'with_front' => false)
  4. // or if you're using Toolset then turn on permalink rewrite and add /%variable% as the custom rewrite
  5.  
  6. add_filter('post_link', 'location_permalink', 10, 3); // Call it what you want
  7. add_filter('post_type_link', 'location_permalink', 10, 3); // Call it what you want
  8.  
  9. function location_permalink($permalink, $post_id, $leavename) { // Make sure your name matches the filter name above
  10. if (strpos($permalink, '%state%') === FALSE) return $permalink; // The variable you add to your rewrite URL, here we are using %state%
  11.  
  12. // Get post
  13. $post = get_post($post_id);
  14. if (!$post) return $permalink;
  15.  
  16. // Get taxonomy terms
  17. $terms = wp_get_object_terms($post->ID, 'state'); // Change 'state' to your tax slug, like 'flower'
  18. if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
  19. else $taxonomy_slug = 'unknown'; // Change 'unknown' to what you want to output when nothing is selected
  20.  
  21. return str_replace('%state%', $taxonomy_slug, $permalink); // Change the variable here too
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement