在WordPress中,我们可以使用自定义文章类型(Custom Post Type)来创建不同类型的文章。默认情况下,WordPress会为每个自定义文章类型分配一个默认的文章模板(single.php)。
以下是一个示例代码,演示如何创建一个自定义文章类型,并将其与默认的文章模板关联起来:
// 创建自定义文章类型
function create_custom_post_type() {
register_post_type('custom_post', // 自定义文章类型的名称
array(
'labels' => array(
'name' => __('Custom Posts'), // 自定义文章类型的名称
'singular_name' => __('Custom Post') // 自定义文章类型的单数形式名称
),
'public' => true, // 公开可见
'has_archive' => true, // 启用归档页面
'supports' => array(
'title', // 标题
'editor', // 编辑器
'thumbnail', // 特色图像
'excerpt' // 摘要
),
'template' => array(
array('core/paragraph', array( // 使用默认的文章模板
'placeholder' => 'Write something...'
))
)
)
);
}
add_action('init', 'create_custom_post_type');
在上面的代码中,我们使用register_post_type
函数创建了一个名为custom_post
的自定义文章类型。我们指定了一些基本的参数,如名称、单数形式名称、公开可见性、归档页面支持等。最重要的是,我们在template
参数中指定了一个包含默认文章模板的数组。
请注意,上面的代码只是一个示例,你可以根据需要进行修改和扩展。你可以根据自己的需求为自定义文章类型添加更多的参数和支持。
希望这可以帮助到你!