我在WordPress中创建了自定义帖子类型,如何添加自定义摘录并在其中添加字段。自定义帖子类型保存在同一wp_posts表中。和添加选项显示所有字段。但是现在我想在此添加自定义摘录字段。我有任何WordPress功能要添加摘录。任何人都可以帮助!
我希望您通过在主题 function.php 文件中添加函数 register_post_type() 来创建自定义帖子类型。如果是,您只需使用“支持”更新您的代码。然后转到屏幕选项并单击“摘录”。
$args = array(
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'book', $args );
Run Code Online (Sandbox Code Playgroud)
或者你也可以添加以下代码
add_action( 'init', 'my_add_excerpts_to_pages' );
function my_add_excerpts_to_pages() {
add_post_type_support( 'page', 'excerpt' ); //change page with your post type slug.
}
Run Code Online (Sandbox Code Playgroud)
如何在 WordPress 中的自定义帖子类型中添加摘录?
示例 1:
<?php
/**
* Enables the Excerpt meta box in post type edit screen.
*/
function wpcodex_add_excerpt_support_for_post() {
add_post_type_support( 'your post type slug name here', 'excerpt' );
}
add_action( 'init', 'wpcodex_add_excerpt_support_for_post' );
?>
Run Code Online (Sandbox Code Playgroud)
更多详细信息请参见:https ://codex.wordpress.org/Function_Reference/add_post_type_support
示例 2:
<?php
add_action( 'init', 'create_testimonial_posttype' );
function create_testimonial_posttype(){
register_post_type( 'testimonials',
array(
'labels' => array(
'name' => __( 'Testimonials' ),
'singular_name' => __( 'Testimonial' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'clients'),
'supports' => array('title','thumbnail','editor','page-attributes','excerpt'),
)
);
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9605 次 |
| 最近记录: |