Skip to content

Instantly share code, notes, and snippets.

@jrthib
Created April 24, 2013 18:36
Show Gist options
  • Save jrthib/5454421 to your computer and use it in GitHub Desktop.
Save jrthib/5454421 to your computer and use it in GitHub Desktop.
Wordpress custom post type for hero stories
<?php
add_action('init', 'hero_story_init');
function hero_story_init() {
$args = array(
'label' => __('Hero Stories'),
'singular_label' => __('Hero Story'),
'public' => true,
'show_ui' => true, // UI in admin panel
'_builtin' => false, // It's a custom post type, not built in!
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'rewrite' => array("slug" => ""), // Permalinks format
'supports' => array('title', 'editor', 'thumbnail'),
'menu_icon' => '/path/to/hero_story_icon.png', // 16px16
'exclude_from_search' => true,
'menu_position' => 6//,
//'taxonomies' => array('category')
);
register_post_type( 'herostory' , $args );
add_new_rules('herostory');
flush_rewrite_rules( false );
}
function add_new_rules($ptype){
global $wp_rewrite;
$rewrite_rules = $wp_rewrite->generate_rewrite_rules("$ptype/");
$rewrite_rules["$ptype/?$"] = 'index.php?paged=1';
foreach($rewrite_rules as $regex => $redirect)
{
if(strpos($redirect, 'attachment=') === false)
{
$redirect .= "&amp;amp;amp;amp;amp;post_type=$ptype";
}
if(0 < preg_match_all('@\$([0-9])@', $redirect, $matches))
{
for($i = 0; $i < count($matches[0]); $i++)
{
$redirect = str_replace($matches[0][$i], '$matches['.$matches[1][$i].']', $redirect);
}
}
$wp_rewrite->add_rule($regex, $redirect, 'top');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment