url rewriting - Custom Post Type not using correct page template

I've been having trouble lately in trying to figure out why this custom post type is not using the correct template

I've been having trouble lately in trying to figure out why this custom post type is not using the correct template file. The custom post type is called 'job' and it has a rewrite on it for the slug to be 'careers'. Any time I make a new job posting I get brought to our blog page, but the url path will be correct. I installed a plugin to see what template was currently showing and it's displaying our home.php template.

The weird thing is that if I go back into the job post and change the permalink to anything else, even just removing the trailing '/' the link will work correctly and pull the correct page template of 'single-job'.

I also noticed that if I remove the slug rewriting WordPress will also use the correct template. Is anyone able to provide any advice on what could be happening here and how to fix it? I have tried flushing my rewrite rules as well, no luck.

Here's my code below:

add_action('init', 'create_custom_post_types');

function create_custom_post_types() {

    $job_labels = array(
        'name' => _x( 'Jobs', 'Post Type General Name', 'textdomain' ),
        'singular_name' => _x( 'Job', 'Post Type Singular Name', 'textdomain' ),
        'menu_name' => _x( 'Jobs', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar' => _x( 'Job', 'Add New on Toolbar', 'textdomain' ),
        'archives' => __( 'Job Archives', 'textdomain' ),
        'attributes' => __( 'Job Attributes', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Job:', 'textdomain' ),
        'all_items' => __( 'All Jobs', 'textdomain' ),
        'add_new_item' => __( 'Add New Job', 'textdomain' ),
        'add_new' => __( 'Add New', 'textdomain' ),
        'new_item' => __( 'New Job', 'textdomain' ),
        'edit_item' => __( 'Edit Job', 'textdomain' ),
        'update_item' => __( 'Update Job', 'textdomain' ),
        'view_item' => __( 'View Job', 'textdomain' ),
        'view_items' => __( 'View Jobs', 'textdomain' ),
        'search_items' => __( 'Search Job', 'textdomain' ),
        'not_found' => __( 'Not found', 'textdomain' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
        'featured_image' => __( 'Featured Image', 'textdomain' ),
        'set_featured_image' => __( 'Set featured image', 'textdomain' ),
        'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
        'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
        'insert_into_item' => __( 'Insert into Job', 'textdomain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Job', 'textdomain' ),
        'items_list' => __( 'Jobs list', 'textdomain' ),
        'items_list_navigation' => __( 'Jobs list navigation', 'textdomain' ),
        'filter_items_list' => __( 'Filter Jobs list', 'textdomain' ),
    );
    $rewrite = array(
        'slug' => 'careers',
        'with_front' => false,
        'pages' => true,
        'feeds' => true,
    );
    $job_args = array(
        'label' => __( 'Job', 'textdomain' ),
        'description' => __( '', 'textdomain' ),
        'labels' => $job_labels,
        'menu_icon' => 'dashicons-category',
        'supports' => array('title', 'editor', 'excerpt'),
        'taxonomies' => array(),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => false,
        'hierarchical' => true,
        'exclude_from_search' => false,
        'show_in_rest' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'rewrite' => $rewrite,
    );

    register_post_type( 'job' , $job_args );

    flush_rewrite_rules();
}

I've been having trouble lately in trying to figure out why this custom post type is not using the correct template file. The custom post type is called 'job' and it has a rewrite on it for the slug to be 'careers'. Any time I make a new job posting I get brought to our blog page, but the url path will be correct. I installed a plugin to see what template was currently showing and it's displaying our home.php template.

The weird thing is that if I go back into the job post and change the permalink to anything else, even just removing the trailing '/' the link will work correctly and pull the correct page template of 'single-job'.

I also noticed that if I remove the slug rewriting WordPress will also use the correct template. Is anyone able to provide any advice on what could be happening here and how to fix it? I have tried flushing my rewrite rules as well, no luck.

Here's my code below:

add_action('init', 'create_custom_post_types');

function create_custom_post_types() {

    $job_labels = array(
        'name' => _x( 'Jobs', 'Post Type General Name', 'textdomain' ),
        'singular_name' => _x( 'Job', 'Post Type Singular Name', 'textdomain' ),
        'menu_name' => _x( 'Jobs', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar' => _x( 'Job', 'Add New on Toolbar', 'textdomain' ),
        'archives' => __( 'Job Archives', 'textdomain' ),
        'attributes' => __( 'Job Attributes', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Job:', 'textdomain' ),
        'all_items' => __( 'All Jobs', 'textdomain' ),
        'add_new_item' => __( 'Add New Job', 'textdomain' ),
        'add_new' => __( 'Add New', 'textdomain' ),
        'new_item' => __( 'New Job', 'textdomain' ),
        'edit_item' => __( 'Edit Job', 'textdomain' ),
        'update_item' => __( 'Update Job', 'textdomain' ),
        'view_item' => __( 'View Job', 'textdomain' ),
        'view_items' => __( 'View Jobs', 'textdomain' ),
        'search_items' => __( 'Search Job', 'textdomain' ),
        'not_found' => __( 'Not found', 'textdomain' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
        'featured_image' => __( 'Featured Image', 'textdomain' ),
        'set_featured_image' => __( 'Set featured image', 'textdomain' ),
        'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
        'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
        'insert_into_item' => __( 'Insert into Job', 'textdomain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Job', 'textdomain' ),
        'items_list' => __( 'Jobs list', 'textdomain' ),
        'items_list_navigation' => __( 'Jobs list navigation', 'textdomain' ),
        'filter_items_list' => __( 'Filter Jobs list', 'textdomain' ),
    );
    $rewrite = array(
        'slug' => 'careers',
        'with_front' => false,
        'pages' => true,
        'feeds' => true,
    );
    $job_args = array(
        'label' => __( 'Job', 'textdomain' ),
        'description' => __( '', 'textdomain' ),
        'labels' => $job_labels,
        'menu_icon' => 'dashicons-category',
        'supports' => array('title', 'editor', 'excerpt'),
        'taxonomies' => array(),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => false,
        'hierarchical' => true,
        'exclude_from_search' => false,
        'show_in_rest' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'rewrite' => $rewrite,
    );

    register_post_type( 'job' , $job_args );

    flush_rewrite_rules();
}
Share Improve this question edited Jun 12, 2019 at 15:51 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Jun 12, 2019 at 15:47 JamesJames 1 2
  • 2 I noticed you're flushing rewrite rules on the init hook, this is bad practice, is a very expensive thing to do, and can cause issues for some plugins and code, does the problem go away if you remove it? – Tom J Nowell Commented Jun 12, 2019 at 15:52
  • Hi Tom, thanks for the answer! I did not know that would cause so many issues. Unfortunately removing it does not address the issue. I removed it and then manually went and pressed save changes under permalink settings just to be sure. Still no change – James Commented Jun 12, 2019 at 16:53
Add a comment  | 

1 Answer 1

Reset to default 0

The problem is caused by flush_rewrite_rules(). Remove that line and the issue should be resolved.

If you call that function on every page load it can cause issues like this with permalinks. This function should only be run once after your theme or plugin is activated (but not on the activation hook, since that's too early). This is because permalinks are persistent and should only be flushed if something has changed.

It's often easier to just manually flush the permalinks by visiting Settings > Permalinks.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745409509a4626464.html

相关推荐

  • url rewriting - Custom Post Type not using correct page template

    I've been having trouble lately in trying to figure out why this custom post type is not using the correct template

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信