I have a custom post type called quotes and a sample url is: http://danconnolly.ie/quotes/go-for-great/
How do I make it so I have the following url: http://danconnolly.ie/state-of-mind-dyer/ ?
/*
Create New Post Type called Quote
*/
add_action('init', 'create_quote_post_type');
function create_quote_post_type()
{
$name = 'Quote';
register_post_type
(
'quote', array
(
'labels' => array
(
'name' => $name . 's',
'singular_name' => $name,
'add_new' => 'Add New ' . $name,
'add_new_item' => 'Add New ' . $name,
'edit' => 'Edit' . $name . 's',
'edit_item' => 'Edit ' . $name,
'new_item' => 'New ' . $name . 's',
'view' => 'View ' . $name,
'view_item' => 'View ' . $name,
'search_items' => 'Search ' . $name . 's',
'not_found' => 'No ' . $name . 's' . ' found',
'not_found_in_trash' => 'No ' . $name . ' s' . ' found in Trash',
'parent' => 'Parent' . $name
),
'supports' => array
(
'title',
// 'editor'
// 'excerpt',
//'trackbacks',
//'custom-fields',
// 'comments',
// 'revisions',
// 'thumbnail',
//'author',
//'page-attributes'
),
'public' => true,
'rewrite' => array('slug' => 'quotes'),
'query_var' => true,
'menu_position' => 4
)
);
}
Fahad Murtaza answers:
Instead of
'rewrite' => array('slug' => 'quotes'),
use
'rewrite' => array('slug' => 'state-of-mind-dyer'),
Fahad Murtaza comments:
Sorry, I am getting back to you quick. The above solution wont work.
Keith Donegan comments:
...This has to be dynamic as I will have more than 1 custom post type
Fahad Murtaza comments:
Understood.
John Cotton answers:
Keith
It's not very clear what you want.
Should "quotes" or "go-for-great" become "state-of-mind"?
Keith Donegan comments:
John, I want to completely remove the custom post type 'Quotes' from the url.
So for example, let's say I created another custom post type called 'Tutorials' and a post called 'How to use MS Paint', the url would be example.com/tutorials/how-to-use-ms-paint/ - I need to remove the /tutorials part.
It's the same with category. If I created a category called 'Tutorials', the url would be example.com/category/tutorials/ but there are plugins to remove the category: http://wordpress.org/extend/plugins/wp-no-category-base/
I need to remove the Custom Post Type name from the url so I am left with example.com/i-am-a-custom-post/!
John Cotton comments:
Can't you just have %postname% as your permalink (in Settings > Permalinks on the dashboard)?
I should caveat the above with "that's not an advisable thing to do for performance reasons", but it would give you what you want...
John Cotton comments:
If the answer to my question is "I can't do that because other links would break..." (I saw your note to Luis), then there is a way of doing this, however, it requires a custom field added to each quote custom post and then some code in the save for that post.
Pseudo code would be:
function called_on_post_save_action() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_rewrite_rules( $rules ) {
$newrules = array();
foreach( quote post ) {
// Get custom field with desired url stored in it
// Add a new rule that rewrites that specific url to the actual post
}
return $newrules + $rules;
}
add_action('rewrite_rules_array', 'add_rewrite_rules');
That's the structure.
You need to do that to prevent WordPress having to check each custom url against a potentially huge list.
Luis Cordova answers:
no that is not right,
'rewrite' => array('slug' => 'state-of-mind-dyer'),
will not work, as you want to remove completely the string in between ...
why do you want to do that though? are you conscious that you can create conflicts between pages and quotes?
If you want to do it disregarding I advice you use a simple permalink plugin to support the CPT as url for pages is rendered
Keith Donegan comments:
Hi,
I have inherited a site with alot of pages that really shouldn't be pages and when I create a custom loop list the pages, pagination breaks. So I want the exact url structure to stay the same.
Luis Cordova comments:
right there are some plugins for permalinks that will keep the same exact structure, but they have to be configured carefully