[wp-trac] [WordPress Trac] #9466: Not work %tag% in permalinks

WordPress Trac wp-trac at lists.automattic.com
Sun Apr 5 15:26:46 GMT 2009


#9466: Not work %tag% in permalinks
--------------------------+-------------------------------------------------
 Reporter:  gashek        |       Owner:  ryan         
     Type:  defect (bug)  |      Status:  new          
 Priority:  normal        |   Milestone:  Unassigned   
Component:  Permalinks    |     Version:  2.7.1        
 Severity:  normal        |    Keywords:  permalink tag
--------------------------+-------------------------------------------------
 function get_permalink($id = 0, $leavename = false) not support %tag%

 My variant this function:
 function get_permalink($id = 0, $leavename = false) {
         $rewritecode = array(
                 '%year%',
                 '%monthnum%',
                 '%day%',
                 '%hour%',
                 '%minute%',
                 '%second%',
                 $leavename? '' : '%postname%',
                 '%post_id%',
                 '%category%',
     '%tag%',
                 '%author%',
                 $leavename? '' : '%pagename%',
         );

         if ( is_object($id) && isset($id->filter) && 'sample' ==
 $id->filter )
                 $post = $id;
         else
                 $post = &get_post($id);

         if ( empty($post->ID) ) return false;

         if ( $post->post_type == 'page' )
                 return get_page_link($post->ID, $leavename);
         elseif ($post->post_type == 'attachment')
                 return get_attachment_link($post->ID);

         $permalink = get_option('permalink_structure');

         if ( '' != $permalink && !in_array($post->post_status,
 array('draft', 'pending')) ) {
                 $unixtime = strtotime($post->post_date);

                 $category = '';
                 if ( strpos($permalink, '%category%') !== false ) {
                         $cats = get_the_category($post->ID);
                         if ( $cats ) {
                                 usort($cats, '_usort_terms_by_ID'); //
 order by ID
                                 $category = $cats[0]->slug;
                                 if ( $parent = $cats[0]->parent )
                                         $category =
 get_category_parents($parent, false, '/', true) . $category;
                         }
                         // show default category in permalinks, without
                         // having to assign it explicitly
                         if ( empty($category) ) {
                                 $default_category = get_category(
 get_option( 'default_category' ) );
                                 $category = is_wp_error( $default_category
 ) ? '' : $default_category->slug;
                         }
                 }

      // add tag to permalink
      $tag = '';
                 if ( strpos($permalink, '%tag%') !== false ) {
      $tags = get_the_tags($post->ID);
                         if ( !empty( $tags ) ) {
         usort($tags, '_usort_terms_by_ID'); // order by ID
                                 $tag = $tags[0]->slug;
                         }
                 }

                 $author = '';
                 if ( strpos($permalink, '%author%') !== false ) {
                         $authordata = get_userdata($post->post_author);
                         $author = $authordata->user_nicename;
                 }

                 $date = explode(" ",date('Y m d H i s', $unixtime));
                 $rewritereplace =
                 array(
                         $date[0],
                         $date[1],
                         $date[2],
                         $date[3],
                         $date[4],
                         $date[5],
                         $post->post_name,
                         $post->ID,
                         $category,
                         $tag,
                         $author,
                         $post->post_name,
                 );
                 $permalink = get_option('home') .
 str_replace($rewritecode, $rewritereplace, $permalink);
                 $permalink = user_trailingslashit($permalink, 'single');
                 return apply_filters('post_link', $permalink, $post,
 $leavename);
         } else { // if they're not using the fancy permalink option
                 $permalink = get_option('home') . '/?p=' . $post->ID;
                 return apply_filters('post_link', $permalink, $post,
 $leavename);
         }
 }

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/9466>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list