Last active
March 13, 2018 16:08
-
-
Save greghunt/32291bf60d07ed00a609 to your computer and use it in GitHub Desktop.
No WP orphans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function no_orphans( $title ) { | |
global $post; | |
if( $title == $post->post_title ){ | |
//Take apart | |
$title_words = explode(' ', $title); | |
$tile_without_last_word = array_slice($title_words, 0, -1); | |
$last_word = array_slice($title_words, -1, 1); | |
//Put back together | |
$new_title = implode(' ', $tile_without_last_word); | |
//add in non-breaking space | |
$new_title .= ' ' . $last_word[0]; | |
$title = $new_title; | |
} | |
return $title; | |
} | |
add_filter( 'the_title', __NAMESPACE__ . '\\no_orphans', 10, 2 ); |
A jQuery equivalent:
$(".title").each(function(){
var words = $(this).text().trim().split(" ");
if( words.length > 1 ) {
var firstWord = words.shift();
var lastWord = words.pop();
var formattedText = firstWord + '\xa0' + words.join(" ") + '\xa0' + lastWord;
$(this).text(formattedText);
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice :)