Custom post type rewrite with_front

admin2025-01-07  8

What I'm setting up, I thought would be quite simple. In fact, I'm sort of amazed that the answer didn't just pop up after a half hour of searching here.

Here is the code to create my CPT:

register_post_type('headline', array(
'labels'                => array(
    'name'                  => 'Headlines',
    'singular_name'         => 'Headline',
    'add_new'               => 'Add Headline',
    'add_new_item'          => 'Add Headline',
    'edit_item'             => 'Edit Headline',
    'new_item'              => 'New Headline',
    'all_items'             => 'All Headlines',
    'view_item'             => 'View Headline',
    'search_items'          => 'Search Headlines',
    'not_found'             =>  'No matching headlines found',
    'not_found_in_trash'    => 'No matching headlines found in Trash', 
    'parent_item_colon'     => '',
    'menu_name'             => 'Headlines'
    ),
'description'           => 'Headlines from around the world.',
'public'                => true,
'supports'              => array('title', 'editor', 'author', 'excerpt', 'thumbnail', 'trackbacks', 'comments', 'revisions'),
'taxonomies'            => array('category', 'post_tag'),
'register_meta_box_cb'  => 'DfePostTypesHeadline::add_meta_boxes',
'has_archive'           => true,
'rewrite'               => array(
    'with_front'    => false
),
'can_export'            => true
));

The problem is the "with_front" call. Or rather, with the rewrite rules themselves. I've got my flush_rewrite_rules() call happening on plugin activation/deactivation. And I can see that the .htaccess file is getting rewritten, so it's working. But WordPress still cannot recognize my new permalink structure.

I'm not sure if this is relevant or not, but my register_post_type call is happening on the "init" hook. I'm wondering if a different hook is necessary?

/* Giddyup */
public function DFEPostTypes() {
    add_action( 'init', 'DFEPostTypes::register_content_types' );
    add_action( 'admin_menu', 'DFEPostTypes::admin_pages' );
    add_action( 'admin_enqueue_scripts', 'DFEPostTypes::admin_scripts' );
    add_filter( 'template_include', 'DFEPostTypes::template' );
    // Flush rewrite rules:
    register_activation_hook( __FILE__, 'DFEPostTypes::flush_rewrite' );
    register_deactivation_hook( __FILE__, 'DFEPostTypes::flush_rewrite' );
}

register_content_types() is the function that handles dynamically calling and registering the content types I work with.

What I'm setting up, I thought would be quite simple. In fact, I'm sort of amazed that the answer didn't just pop up after a half hour of searching here.

Here is the code to create my CPT:

register_post_type('headline', array(
'labels'                => array(
    'name'                  => 'Headlines',
    'singular_name'         => 'Headline',
    'add_new'               => 'Add Headline',
    'add_new_item'          => 'Add Headline',
    'edit_item'             => 'Edit Headline',
    'new_item'              => 'New Headline',
    'all_items'             => 'All Headlines',
    'view_item'             => 'View Headline',
    'search_items'          => 'Search Headlines',
    'not_found'             =>  'No matching headlines found',
    'not_found_in_trash'    => 'No matching headlines found in Trash', 
    'parent_item_colon'     => '',
    'menu_name'             => 'Headlines'
    ),
'description'           => 'Headlines from around the world.',
'public'                => true,
'supports'              => array('title', 'editor', 'author', 'excerpt', 'thumbnail', 'trackbacks', 'comments', 'revisions'),
'taxonomies'            => array('category', 'post_tag'),
'register_meta_box_cb'  => 'DfePostTypesHeadline::add_meta_boxes',
'has_archive'           => true,
'rewrite'               => array(
    'with_front'    => false
),
'can_export'            => true
));

The problem is the "with_front" call. Or rather, with the rewrite rules themselves. I've got my flush_rewrite_rules() call happening on plugin activation/deactivation. And I can see that the .htaccess file is getting rewritten, so it's working. But WordPress still cannot recognize my new permalink structure.

I'm not sure if this is relevant or not, but my register_post_type call is happening on the "init" hook. I'm wondering if a different hook is necessary?

/* Giddyup */
public function DFEPostTypes() {
    add_action( 'init', 'DFEPostTypes::register_content_types' );
    add_action( 'admin_menu', 'DFEPostTypes::admin_pages' );
    add_action( 'admin_enqueue_scripts', 'DFEPostTypes::admin_scripts' );
    add_filter( 'template_include', 'DFEPostTypes::template' );
    // Flush rewrite rules:
    register_activation_hook( __FILE__, 'DFEPostTypes::flush_rewrite' );
    register_deactivation_hook( __FILE__, 'DFEPostTypes::flush_rewrite' );
}

register_content_types() is the function that handles dynamically calling and registering the content types I work with.

Share Improve this question edited Mar 3, 2014 at 16:31 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Mar 3, 2014 at 16:04 Tom BelknapTom Belknap 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have to register your post type before you flush the rules. Your init action doesn't run on plugin activation, so your flush_rewrite method should call register_content_types first before flushing the rules.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257695a457.html

最新回复(0)