Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

hi and thanks for reading.

my issue is, i want to insert the specific author into my custom post type article. my code looks like this

register_post_type('charts', array( 'label' => 'Charts','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => false,'rewrite' => array('slug' => '/charts/author'),'query_var' => true,'supports' => array('title','editor','trackbacks','custom-fields','comments','author',),'labels' => array ( 
'name' => 'Charts',
'singular_name' => 'Charts',
'menu_name' => 'Charts',
'add_new' => 'Add Charts',
),) );

while i want 'rewrite' => array('slug' => '/charts/author') to show example.com/charts/%author%/...

any idea on how to realize this?

bless jnz

share|improve this question

1 Answer

up vote 4 down vote accepted

i found a solution and share it, cause its nice to be nice. hope this is all correct, otherwise feel free to correct me. this works for me and is based on a solution by Jonathan Brinley

first create your custom post type and set it up like this (this is just an example, remeber to make it fit for your own needs.. important is the setting for the slug)

register_post_type(
    'charts', 
    array(  
        'label' => 'Whatever',
        'description' => '',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'rewrite' => array('slug' => '/whatever/%author%'),
        'query_var' => true,
        'supports' => array(
            'title',
            'editor',
            'trackbacks',
            'custom-fields',
            'comments',
            'author'
        ) 
    ) 
);

then you need to set up a function for your filter (in functions.php)

function my_post_type_link_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
   if ( strpos('%author%', $post_link) === FALSE ) {
      $post = &get_post($id);
      $author = get_userdata($post->post_author);
      return str_replace('%author%', $author->user_nicename, $post_link);
   }
}

and activate the filter (also in functions.php)

add_filter('post_type_link', 'my_post_type_link_filter_function', 1, 3);

like i said, i dont know, if this is all super correct, but it works for me :)

share|improve this answer
That’s an interesting solution. Please mark your question as answered. – toscho Feb 25 '12 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.