首页 问答 正文

如何在wordpress中创建自定义文章类型?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 147 次

在WordPress中创建自定义文章类型可以通过以下步骤完成:

  1. 打开functions.php文件

在wp-content/themes/your-theme-name/functions.php文件中添加以下代码来创建自定义文章类型:

add_action( 'init', 'create_post_type' );
function create_post_type() {
   register_post_type( 'custom_post',
      array(
         'labels' => array(
            'name' => __( 'Custom Posts' ),
            'singular_name' => __( 'Custom Post' )
         ),
         'public' => true,
         'has_archive' => true,
         'rewrite' => array('slug' => 'custom-post'),
         'supports' => array('title', 'editor', 'thumbnail', 'excerpt')
      )
   );
} 

在这个例子中,'custom_post'是自定义文章类型的名称,'Custom Posts'是文章类型的复数形式名称,'Custom Post'是文章类型的单数形式名称,'public'参数决定文章类型是否公开展示,'has_archive'参数决定是否需要存档页,'rewrite'参数定义如何重定向URL,'supports'参数定义文章类型支持哪些功能,例如标题,编辑器,缩略图和摘录。

  1. 保存并刷新您的站点

上述代码添加到functions.php此后,您需要保存并刷新您的站点,以使新文章类型生效。打开WordPress后台并在左侧的菜单中找到“文章类型”,您现在应该能看到一个新的自定义文章类型。

这是一个简单的示例,更复杂的自定义文章类型可以添加更多的选项和自定义功能。

大家谈论
    我的见解