What I want to do is… on a per page/post basis I want to be able to have the H1 say something different then the Title…
This should be such a simple thing, but I’ve been looking around for an option and searching google for a solution for an hour now, and I can’t find anything.
Is there a way to do this in WordPress without hacking the code?
Or if not, what is a clean way to do this with code that will not get overwritten when I update WordPress?
p.s. im using the Dynamik/Geneisis theme.
Yes! Custom fields: http://codex.wordpress.org/Custom_Fields
You can create alternate post titles via the post editor, and then you can display them with some minor adjustments to the single.php
and index.php
templates (or {$post_type}-archive.php
templates, if you have them). For example:
<?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 has a great introduction on Smashing Magazine here: http://wp.smashingmagazine.com/2010/04/29/extend-wordpress-with-custom-fields/
EDIT
If you’d like to make this update-proof, I would suggest making a handy little plugin. For something to try, take the code below and replace “YOUR_CUSTOM_FIELD_HERE” with your custom field, and then it will filter the template tag the_title();
to replace it with that field (if not filled in, returns the post title). I haven’t tested this code, but it should work:
<?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);
?>
To try it out, take the code and give it a name (say, custom-title.php
) and drop it in your site’s wp-content/plugins
folder and activate it. Let me know if it works out!