我想为我创建的某些帖子添加一个自定义字段,这些帖子会自动为我发布的一些短阅读添加一个自定义前缀,这些帖子也会添加到它们自己的父类别中。
前缀将是“[30秒读取]:”-它将出现在我指定的所有帖子的开头。
我对自定义字段了解不多,因此不知道如何执行此操作。
我想为我创建的某些帖子添加一个自定义字段,这些帖子会自动为我发布的一些短阅读添加一个自定义前缀,这些帖子也会添加到它们自己的父类别中。
前缀将是“[30秒读取]:”-它将出现在我指定的所有帖子的开头。
我对自定义字段了解不多,因此不知道如何执行此操作。
可能有几种方法可以解决这个问题。这里有一种方法可以使用复选框为单个帖子启用前缀,并在the_title
在任何时候添加前缀the_title()
这些职位都需要。
post
编辑屏幕function wpd_title_prefix_register_meta_box() {
add_meta_box(
\'wpd-title-prefix\',
\'Title Has Prefix?\',
\'wpd_title_prefix_meta_box\',
\'post\',
\'normal\',
\'high\'
);
}
add_action( \'add_meta_boxes\', \'wpd_title_prefix_register_meta_box\' );
渲染元框function wpd_title_prefix_meta_box( $post ){
$checked = get_post_meta( $post->ID, \'_wpd_title_prefix\', true );
?>
<input type="checkbox" name="wpd-title-prefix" <?php checked( $checked ); ?> /> Yes
<?php
}
保存元框值function wpd_title_prefix_save_meta( $post_id ) {
if( isset( $_POST[\'wpd-title-prefix\'] ) ){
update_post_meta( $post_id, \'_wpd_title_prefix\', 1 );
} else {
delete_post_meta( $post_id, \'_wpd_title_prefix\' );
}
}
add_action( \'save_post\', \'wpd_title_prefix_save_meta\' );
the_title
并将前缀添加到选中的帖子中function wpd_title_prefix_filter( $title, $post_id ) {
if( $checked = get_post_meta( $post_id, \'_wpd_title_prefix\', true ) ){
$title = \'[30 Second Read]: \' . $title;
}
return $title;
}
add_filter( \'the_title\', \'wpd_title_prefix_filter\', 10, 2 );