有一种方法可以在不安装插件的情况下添加这个简单的功能。
除非我读错了,否则您要做的就是添加一个新的元框,您可以在其中插入子标题并将其显示在该帖子上。
在 functions.php 中,添加它以创建一个元框来容纳您的子标题字段
function your_sub_title() {
add_meta_box('your_sub_title_metabox', 'Edit Sub Title', 'your_sub_title_metabox', 'post', 'normal', 'default'); ## Adds a meta box to post type
}
现在也在 functions.php 中添加新字段的代码
function your_sub_title_metabox() {
global $post; ## global post object
wp_nonce_field( plugin_basename( __FILE__ ), 'your_sub_title_nonce' ); ## Create nonce
$subtitle = get_post_meta($post->ID, 'sub_title', true); ## Get the subtitle
?>
<p>
<label for="sub_title">Sub Title</label>
<input type="text" name="sub_title" id="sub_title" class="widefat" value="<?php if(isset($subtitle)) { echo $subtitle; } ?>" />
</p>
<?php
}
接下来要做的是创建保存功能。同样在 functions.php
function sub_title_save_meta($post_id, $post) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return false; ## Block if doing autosave
if ( !current_user_can( 'edit_post', $post->ID )) {
return $post->ID; ## Block if user doesn't have priv
}
if ( !wp_verify_nonce( $_POST['your_sub_title_nonce'], plugin_basename(__FILE__) )) {
} else {
if($_POST['sub_title']) {
update_post_meta($post->ID, 'sub_title', $_POST['sub_title']);
} else {
update_post_meta($post->ID, 'sub_title', '');
}
}
return false;
}
add_action('save_post', 'sub_title_save_meta', 1, 2);
然后,就在 single.php 模板中的 the_title() 下方
...
the_title();
$subtitle = get_post_meta(get_the_ID(), 'sub_title', true);
if(isset($subtitle)) {
echo $subtitle;
}