You can try extracting the content of the first H1 tag from your post.
function get_first_h1(){
//Get filtered content of post
ob_start();
the_content();
$content = ob_get_clean();
//Define matching pattern: content of h1 tags
$h1_pattern = "~<h1>(.*)</h1>~";
//Variable to hold results
$header_matches = array();
//Search content for pattern
preg_match( $h1_pattern, $content, $header_matches );
//First match will be 2nd item in array (first is entire content, if any matches)
if ( count( $header_matches ) > 1 ){
return $header_matches[1];
}
//Fallback: return post title
return get_the_title();
}
Notes:
- That function should be saved in functions.php, a custom plugin, or a file included in one of those
- Result of that function may be echoed out or used as part of another function , i.e. echo get_first_h1();
- That function, as written, must be used within a WordPress Loop
Personally, I like @Jacob Peattie's suggestion above better. Create a custom field to store the extra title field you want. I am providing this solution in case you must do it this way, i.e. you have 1,000's of posts and don't want to go set the title fields.
title
in some places and a shorter one elsewhere. Many sites use SEO plugins such as Yoast to make the<title>
tag different than the WPtitle
.<title>
element? The name of the post in the back-end? The meta title? If you're not usingthe_title()
then what are you using the title for that requires it to be shorter?