另一个解决方案是为项目页面注册一个元框,它允许您为每个页面键入任何想要的类名…
function add_project_page_metabox() {
add_meta_box(
\'project_page_meta\', // $id
\'Project Meta\',
// $title
\'ppm_callback\', // $callback
\'page\', // $post_type
\'side\', // $context
\'low\' // $priority
);
}
add_action( \'admin_init\', \'add_project_page_metabox\' );
function ppm_callback() {
global $post;
$project_class = get_post_meta($post->ID,\'project_class\',TRUE);
wp_nonce_field( $_POST[\'page_meta_noncename\'],__FILE__);
/* some UI styling */ ?>
<style type="text/css">
#project_page_meta {display:block;margin:0 0 12px;float:none;}
#project_page_meta label {display:block;margin:0 1em .6em 0;}
#project_page_meta input {margin:0 0 .25em;clear:both;float:none;max-width:97%;}
</style>
<label>Add class name</label>
<input class="project_class" size="35" type="text" name="project_class" value="<?php if($project_class) { echo $project_class; } ?>"/>
<?php
}
// process the meta
function project_meta_save($post_id) {
if(!current_user_can(\'edit_post\', $post_id)) { return $post_id; }
$accepted_fields[\'page\'] = array(
\'project_class\'
);
foreach($accepted_fields[\'page\'] as $key){
$custom_field = $_POST[$key];
//If no data is entered
if (is_null($custom_field)) { delete_post_meta($post_id, $key); }
elseif (isset($custom_field) && !is_null($custom_field))
{ update_post_meta($post_id,$key,$custom_field); }
else { add_post_meta($post_id, $key, $custom_field, TRUE); }
}
return $post_id;
}
add_action(\'save_post\', \'project_meta_save\', 3, 1 );
…然后在页面中使用此选项。类似php
class="<?php echo get_post_meta($post->ID,\'project_class\',TRUE); ?>"