Advertisement
keha76

WordPress Hierarchical Taxonomy Permalinks

Sep 19th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.45 KB | None | 0 0
  1. <?php
  2. /**
  3.  * WordPress Custom Post Type and Hierarchical Taxonomy Permalinks
  4.  *
  5.  * Ever wanted to have custom taxonomy permalinks that reflect parent/child relation?
  6.  * Then this is for you. Still in development though.
  7.  *
  8.  * @version 1.0b
  9.  * @author Kenth Hagström
  10.  * @link http://www.facebook.com/keha76
  11.  * @license GNU General Public License v2
  12.  *
  13.  * Last updated 19 sep 2013, 15:30(GMT+1)
  14.  *
  15.  * KNOWN BUGS:
  16.  * - When a 'photo' is published without any taxonomy, the URL url.tld/photos/john does not work.
  17.  * - The full URL url.tld/photos/family/kids/john/ has an unwanted trailing slash.
  18.  *
  19.  * These URL structures work:
  20.  * url.tld/photos/ loads archive-photo.php
  21.  * url.tld/photos/family/ loads taxonomy-album.php
  22.  * url.tld/photos/family/kids/ loads taxonomy-album.php
  23.  * url.tld/photos/family/kids/john loads single-photo.php
  24.  * url.tld/photos/page/2 loads page 2 with archive-photo.php
  25.  *
  26.  * If you try this code, remember to flush your permalinks. Go to 'settings->permalinks'.
  27.  *
  28. **/
  29. function keha_register_photo_tax_album() {
  30.        
  31.     $labels = array(
  32.         'name'              => _x( 'Albums', 'taxonomy general name', 'blank' ),
  33.         'singular_name'     => _x( 'Album', 'taxonomy singular name', 'blank' ),
  34.         'search_items'      => __( 'Search Albums', 'blank' ),
  35.         'all_items'         => __( 'All Albums', 'blank' ),
  36.         'parent_item'       => __( 'Parent Album', 'blank' ),
  37.         'parent_item_colon' => __( 'Parent Album:', 'blank' ),
  38.         'edit_item'         => __( 'Edit Album', 'blank' ),
  39.         'update_item'       => __( 'Update Album', 'blank' ),
  40.         'add_new_item'      => __( 'Add New Album', 'blank' ),
  41.         'new_item_name'     => __( 'New Album Name', 'blank' ),
  42.         'menu_name'         => __( 'Albums', 'blank' ),
  43.     );  
  44.  
  45.     $rewrite = array(
  46.         'slug' => 'album',
  47.         'with_front' => false,
  48.         'hierarchical' => true
  49.     );
  50.  
  51.     $args = array(
  52.         'hierarchical' => true,
  53.         'labels'       => $labels,
  54.         'show_ui'      => true,
  55.         'query_var'    => true,
  56.         'rewrite'      => $rewrite
  57.     );
  58.     register_taxonomy( 'album', array( 'photo' ), $args );
  59. }
  60. add_action( 'init', 'keha_register_photo_tax_album' );
  61.  
  62. function keha_register_cpt_photo() {
  63.  
  64.     $labels = array(
  65.         'name'               => __( 'Photos', 'blank' ),
  66.         'singular_name'      => _x( 'Photo', 'Photo post type singular', 'blank' ),
  67.         'add_new'            => __( 'Add New', 'blank' ),
  68.         'add_new_item'       => __( 'Add New Photo', 'blank' ),
  69.         'edit_item'          => __( 'Edit Photo', 'blank' ),
  70.         'new_item'           => __( 'New Photo', 'blank' ),
  71.         'all_items'          => __( 'All Photos', 'blank' ),
  72.         'view_item'          => __( 'View Photo', 'blank' ),
  73.         'search_items'       => __( 'Search Photos', 'blank' ),
  74.         'not_found'          => __( 'No photos found', 'blank' ),
  75.         'not_found_in_trash' => __( 'No photos found in Trash', 'blank' ),
  76.         'parent_item_colon'  => '',
  77.         'menu_name'          => __( 'Photos', 'blank' )
  78.     );
  79.  
  80.     $rewrite = array(
  81.         'slug' => 'photos',
  82.         'hierarchical' => true,
  83.         'with_front' => true
  84.     );
  85.  
  86.     $args = array(
  87.         'labels'               => $labels,
  88.         'exclude_from_search'  => false,
  89.         'publicly_queryable'   => true,
  90.         'show_ui'              => true,
  91.         'show_in_nav_menus'    => true,
  92.         'show_in_menu'         => true,
  93.         'show_in_admin_bar'    => true,
  94.         'menu_position'        => 20,
  95.         'menu_icon'            => null, // Handled from CSS
  96.         'capability_type'      => 'post',
  97.         'hierarchical'         => false,
  98.         'public'               => true,
  99.         'supports'             => array( 'title', 'editor', 'thumbnail' ),
  100.         'taxonomies'           => array( 'album' ),
  101.         'rewrite'              => $rewrite,
  102.         'has_archive'          => true,
  103.         'query_var'            => true,
  104.         'can_export'           => true
  105.     );
  106.  
  107.     register_post_type( 'photo', $args );
  108. }
  109. add_action( 'init', 'keha_register_cpt_photo' );
  110.  
  111. function keha_add_rewrite_rules() {
  112.     add_rewrite_rule( '^photos/(.+?)/(.+?)/(.+?)$', 'index.php?album=$matches[1]&album=$matches[2]&photo=$matches[3]', 'top' );
  113.     add_rewrite_rule( '^photos/(.+?)/(.+?)/$', 'index.php?photo=$matches[2]', 'top' );
  114.     add_rewrite_rule( '^photos/(.+?)/(.+?)/(.+?)$', 'index.php?photo=$matches[3]', 'top' );
  115.     add_rewrite_rule( '^photos/(.+?)/(.+?)/?$', 'index.php?album=$matches[2]', 'top' );
  116.     add_rewrite_rule( '^photos/(.+?)$', 'index.php?album=$matches[1]', 'top' );
  117. }
  118. add_action('init', 'keha_add_rewrite_rules');
  119.  
  120. function keha_post_type_link( $post_link, $post, $leavename ) {
  121.  
  122.     global $wp_rewrite;
  123.  
  124.     $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  125.     if ( $draft_or_pending and !$leavename ) {
  126.         return $post_link;
  127.     }
  128.        
  129.     if ( $post->post_type == 'photo' ) {
  130.        
  131.         $post_type_object = get_post_type_object( $post->post_type );
  132.         $post_link = home_url() . '/' . $post_type_object->rewrite['slug'] . '/';
  133.         $parent_dirs = '';
  134.         if ( $terms = get_the_terms( $post->ID, 'album' ) ) {
  135.             foreach ( $terms as $term ) {
  136.                 if ( $term->parent != 0 ) {
  137.                     $dirs = keha_get_taxonomy_parents( $term->term_id, 'album', false, '/', true );
  138.                 } else {
  139.                     $dirs = $term->slug.'/';
  140.                 }
  141.             }
  142.         }
  143.         $post_link = $post_link . $dirs . $post->post_name;
  144.     }
  145.    
  146.     return $post_link;
  147. }
  148. add_filter( 'post_type_link', 'keha_post_type_link', 10, 3 );
  149.  
  150. /**
  151.  * Custom function based on WordPress own get_category_parents()
  152.  * @link http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/category-template.php#L0
  153.  *
  154.  * @param integer $id
  155.  * @param string $taxonomy
  156.  * @param string $link
  157.  * @param string $separator
  158.  * @param string $nicename
  159.  * @param array $visited
  160.  * @return string
  161.  */
  162. function keha_get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
  163.     $chain = '';
  164.     $parent = get_term( $id, $taxonomy, OBJECT, 'raw');
  165.     if ( is_wp_error( $parent ) ) {
  166.         return $parent;
  167.     }
  168.  
  169.     if ( $nicename ){
  170.         $name = $parent->slug;
  171.     } else {
  172.         $name = $parent->name;
  173.     }
  174.  
  175.     if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
  176.         $visited[] = $parent->parent;
  177.         $chain .= keha_get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
  178.     }
  179.  
  180.     if ( $link ) {
  181.         $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
  182.     }else {
  183.         $chain .= $name.$separator;
  184.     }
  185.     return $chain;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement