自定义帖子类型未使用正确的页面模板

时间:2019-06-12 作者:James

我最近一直很难弄清楚为什么这个自定义帖子类型没有使用正确的模板文件。自定义的post类型称为“job”,上面有一个重写,使slug成为“careers”。每当我发布新的招聘信息时,我都会被带到我们的博客页面,但url路径是正确的。我安装了一个插件来查看当前显示的模板和显示我们的主页。php模板。

奇怪的是,如果我回到工作岗位,将永久链接更改为其他任何内容,即使只是删除尾部的“/”,链接也会正常工作,并拉出正确的“单个工作”页面模板。

我还注意到,如果我删除slug重写,WordPress也将使用正确的模板。是否有人能够就这里可能发生的情况以及如何解决问题提供任何建议?我也试过刷新我的重写规则,运气不好。

下面是我的代码:

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();
}

1 个回复
SO网友:Jacob Peattie

问题是由flush_rewrite_rules(). 删除该行,问题应该得到解决。

如果在每次页面加载时调用该函数,则可能会导致永久链接出现类似的问题。此功能只能在主题或插件激活后运行一次(但不能在激活挂钩上运行,因为这太早了)。这是因为永久链接是持久性的,只有在发生更改时才应刷新。

通过访问permalinks,手动刷新permalinks通常更容易。