最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Custom Post Type not using correct page template

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论