可能有几种方法可以解决这个问题。这里有一种方法可以使用复选框为单个帖子启用前缀,并在the_title
在任何时候添加前缀the_title()
这些职位都需要。
1。为开/关复选框添加元框。将元框添加到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\' );
2。滤器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 );