Reputation: 6485
I've recently created several custom post types in a wordpress site, with the code below, they are generating links in the correct form e.g. root/category/id/postname but each link to the full post, pagination or category 404's.
I've tried a number of popular solutions, appending /%category%/%post_id%/ to the url structure, rewriting function names but I'm getting nowhere fast.
With the wordpress permalink structure set to default e.g. root/?page_id=1257 everything works ok.
Any effort to add additional parameters to rewrite (see below) produces "Parse error: syntax error, unexpected ';'" even though no ';' is present.
'rewrite' => array(
'slug' => 'issue')
Any help appreciated - highly confused, very frustrated!
<?php
// CUSTOM POST TYPE 1
add_action('init', 'mjwpress_register');
function mjwpress_register() {
$args = array(
'label' => __('Press'),
'singular_label' => __('Press'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'new_item' => __('New Press Item'),
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'comments', 'revisions', 'page-attributes', 'post-formats')
);
register_taxonomy('press-category', array('article'),
array(
'label' => 'Press Story Category',
'singular_label' => 'press-story-category',
'public' => TRUE,
'show_tagcloud' => TRUE,
'hierarchical' => TRUE,
'query_var' => TRUE,
'menu_position' => 5,
'rewrite' => TRUE)
);
register_post_type( 'mjwpress' , $args );
}
add_action('inthenews_init', 'inthenews_init');
add_action('save_post', 'save_mjwpress_options');
function inthenews_init(){
add_meta_box('newsmeta', 'Press Options', 'mjwpress_meta_options', 'mjwpress', 'normal', 'low');
}
function mjwpress_meta_options(){
global $post;
$custom = get_post_custom($post->ID);
$linkurl = $custom['linkurl'][0];
$linktitle = $custom['linktitle'][0];
?>
<div class='form-wrap'>
<div class='form-field'>
<label for='linkurl'>Link to External Publication:</label>
<input name='linkurl' value='<?php echo $linkurl; ?>' />
<p>E.g. http://www.example.com/article-title.php</p>
</div>
<div class='form-field'>
<label for='linktitle'>Title of External Publication:</label>
<input name='linktitle' value='<?php echo $linktitle; ?>' />
<p>E.g. Lib Dem Voice</p>
</div>
</div>
<?php
}
function save_mjwpress_options(){
global $post;
update_post_meta($post->ID, 'linkurl', $_POST['linkurl']);
update_post_meta($post->ID, 'linktitle', $_POST['linktitle']);
}
?>
Upvotes: 1
Views: 2921
Reputation: 49
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category()) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item', 'post', 'your_custom_post_type');
$query->set('post_type', $post_type);
return $query;
}
}
Upvotes: 1
Reputation: 6485
It turns out the the order taxonomy and post type occur in is important where rewrite is concerned.
See http://mondaybynoon.com/2011/05/20/revisiting-custom-post-types-taxonomies-permalinks-slugs/
<?php
// Links post type...
// Taxonomy
$labels = array(
'name' => 'Brands',
'singular_name' => 'Brand',
'search_items' => 'Search Brands',
'popular_items' => 'Popular Brands',
'all_items' => 'All Brands',
'parent_item' => 'Parent Brand',
'edit_item' => 'Edit Brand',
'update_item' => 'Update Brand',
'add_new_item' => 'Add New Brand',
'new_item_name' => 'New Brand',
'separate_items_with_commas' => 'Separate Brands with commas',
'add_or_remove_items' => 'Add or remove Brands',
'choose_from_most_used' => 'Choose from most used Brands'
);
$args = array(
'label' => 'Brands',
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'cameras/brands', 'with_front' => false ),
'query_var' => true
);
register_taxonomy( 'brands', 'cameras', $args );
//Post type, must come after the taxonomy...
register_post_type( 'cameras',
array(
'labels' => array(
'name' => __( 'Cameras' ),
'singular_name' => __( 'Camera' )
),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'rewrite' => array( 'slug' => 'cameras', 'with_front' => false ),
'has_archive' => true
)
);
?>
Upvotes: 0
Reputation: 805
Whenever this happens to me, I take a look at .htaccess, and quickly realize it wasnt there or the wordpress permalink .htaccess code didn't write itself and needs to be copy/pasted in.
Double check!
Upvotes: 0
Reputation: 26719
I've been fighting this all day long :) Just go to options->Permalinks and save the options, so it will regenerate the rewrite rules in the database. It happens when you have set your permalinks options, and after that registered a custom post type.
If you use cache plugins like W3 Total Cache, flush cache before saving permalinks options.
Also, make sure you have no conflicts in the post type names, use:
'rewrite' => array('slug' => '<some-unique-prefix>')
If this does not help, check this out
Upvotes: 2