我创建了新的帖子类型和分类法。以下是脚本:
add_action(\'init\', \'my_custom_post_type_init\');
function my_custom_post_type_init()
{
$labels = array(
\'name\' => _x(\'Roundabouts\', \'post type general name\'),
\'singular_name\' => _x(\'Roundabout\', \'post type singular name\'),
\'add_new\' => _x(\'Add New\', \'Roundabout\'),
\'add_new_item\' => __(\'Add New Roundabout\'),
\'edit_item\' => __(\'Edit Roundabout\'),
\'new_item\' => __(\'New Roundabout\'),
\'view_item\' => __(\'View Roundabout\'),
\'search_items\' => __(\'Search Roundabouts\'),
\'not_found\' => __(\'No Roundabouts found\'),
\'not_found_in_trash\' => __(\'No Roundabouts found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => 5,
\'rewrite\' => array(\'slug\' => \'%category%\', \'with_front\' => FALSE),
\'taxonomies\' => array( \'post_tag\', \'category\'),
\'supports\' => array(\'title\',\'editor\',\'author\',\'thumbnail\',\'excerpt\',\'comments\', \'thumbnail\')
);
register_post_type(\'roundabouts\',$args);
}
这是永久链接设置,我不允许更改。
/%category%/%postname%/
现在我需要生成permalink,如:
http://my_site/category_name/post_title
虽然我在谷歌上找到了许多文章,但问题如下:
Permalinks 404 with custom post type
Custom Permalinks for Custom Post Types in WordPress 3.0+
custom post type with post_id in permalink structure
但在完成研发之后,我几乎浪费了4个小时的生命;我终于找到了这会以某种方式将我的post permalink转换为我所需要的,但如果我点击了该链接,就会出现404错误。下面是最适合我的上述代码的修改后的脚本:
// Add filter to plugin init function
add_filter(\'post_type_link\', \'gallery_permalink\', 10, 3);
// Adapted from get_permalink function in wp-includes/link-template.php
function gallery_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
\'%year%\',
\'%monthnum%\',
\'%day%\',
\'%hour%\',
\'%minute%\',
\'%second%\',
$leavename? \'\' : \'%postname%\',
\'%post_id%\',
\'%category%\',
\'%author%\',
$leavename? \'\' : \'%pagename%\',
);
if ( \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {
$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;
}
}
$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,
$author,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
} else { // if they\'re not using the fancy permalink option
}
return $permalink;
}
请指导我还能做些什么。