对自定义字段:http://codex.wordpress.org/Custom_Fields
您可以通过帖子编辑器创建备用帖子标题,然后通过对single.php
和index.php
模板(或{$post_type}-archive.php
模板(如果有)。例如:
<?php $post_title = get_post_meta($post->ID, \'Post-Title\', true); //"Post-Title" or whatever you call your custom field
if ($post_title) {
?>
<h1><?php echo $post_title; ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
亚历克斯·丹尼(Alex Denning)在《粉碎》(Smashing)杂志上有一篇精彩的介绍:
http://wp.smashingmagazine.com/2010/04/29/extend-wordpress-with-custom-fields/编辑
如果你想证明这个更新,我建议你制作一个方便的小插件。要进行尝试,请使用下面的代码并将“YOUR\\u CUSTOM\\u FIELD\\u HERE”替换为自定义字段,然后它将过滤模板标记
the_title();
将其替换为该字段(如果未填写,则返回文章标题)。我尚未测试此代码,但它应该可以工作:
<?php
/**
* Plugin Name: Replace Title With Custom Field
* Description: When activated, this plugin replaces the title H1 with a custom field from the dashboard.
* Version: 1.0
* Author: penguin429
* License: GPL2
*/
function replace_title($title, $id) {
$custom_title = get_post_meta($id, \'YOUR_CUSTOM_FIELD_HERE\', true);
$mod_title = $title;
if(!empty($custom_title)) { //if field is filled in, make the title the Custom Field
$mod_title = $custom_title;
} else { //otherwise, return the post Title
$mod_title = $title;
}
return $mod_title;
}
add_filter(\'the_title\', \'replace_title\', 10, 2);
?>
要试用它,请获取代码并为其命名(例如,
custom-title.php
) 并将其放在您网站的
wp-content/plugins
文件夹并将其激活。如果成功了,请告诉我!