WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/01/2010 11:12:26 PM (12 years ago)
Author:
ryan
Message:

Permalinks for custom post types. Props prettyboymp. see #9674

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/link-template.php

    r12789 r12923  
    105105    if ( $post->post_type == 'page' )
    106106        return get_page_link($post->ID, $leavename, $sample);
    107     elseif ($post->post_type == 'attachment')
     107    elseif ( $post->post_type == 'attachment' )
    108108        return get_attachment_link($post->ID);
     109    elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
     110        return get_post_link($post);
    109111
    110112    $permalink = get_option('permalink_structure');
     
    161163
    162164/**
     165 * Retrieve the permalink for a post with a custom post type.
     166 *
     167 * @since 3.0.0
     168 *
     169 * @param int $id Optional. Post ID.
     170 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
     171 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
     172 * @return string
     173 */
     174function get_post_link( $id = 0, $leavename = false, $sample = false  ) {
     175    global $wp_rewrite;
     176
     177    $post = &get_post($id);
     178
     179    if ( is_wp_error( $post ) )
     180        return $post;
     181
     182    $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
     183
     184    $slug = $post->post_name;
     185
     186    if ( !empty($post_link) && ( ( isset($post->post_status) && 'draft' != $post->post_status && 'pending' != $post->post_status ) || $sample ) ) {
     187        $post_link = ( $leavename ) ? $post_link : str_replace("%$post->post_type%", $slug, $post_link);
     188        $post_link = home_url( user_trailingslashit($post_link) );
     189    } else {
     190        $post_type = get_post_type_object($post->post_type);
     191        if ( $post_type->query_var && ( isset($post->post_status) && 'draft' != $post->post_status && 'pending' != $post->post_status ) )
     192            $post_link = "?$post_type->query_var=$slug";
     193        else
     194            $post_link = "?post_type=$post->post_type&p=$post->ID";
     195        $post_link = home_url($post_link);
     196    }
     197
     198    return apply_filters('post_type_link', $post_link, $id);
     199}
     200
     201/**
    163202 * Retrieve permalink from post ID.
    164203 *
Note: See TracChangeset for help on using the changeset viewer.