更新帖子
$all_posts = get_posts(
\'posts_per_page\' => -1,
\'post_type\' => \'post\'
);
foreach ( $all_posts as $single ) {
wp_update_post( array(
\'ID\' => $single->ID,
\'post_title\' => to_title_case( $single->post_title ) // see function below
));
}
将字符串转换为“标题大小写”并且,虽然与WP无关,但为了完整起见:
function to_title_case( $string ) {
/* Words that should be entirely lower-case */
$articles_conjunctions_prepositions = array(
\'a\',\'an\',\'the\',
\'and\',\'but\',\'or\',\'nor\',
\'if\',\'then\',\'else\',\'when\',
\'at\',\'by\',\'from\',\'for\',\'in\',
\'off\',\'on\',\'out\',\'over\',\'to\',\'into\',\'with\'
);
/* Words that should be entirely upper-case (need to be lower-case in this list!) */
$acronyms_and_such = array(
\'asap\', \'unhcr\', \'wpse\', \'wtf\'
);
/* split title string into array of words */
$words = explode( \' \', mb_strtolower( $string ) );
/* iterate over words */
foreach ( $words as $position => $word ) {
/* re-capitalize acronyms */
if( in_array( $word, $acronyms_and_such ) ) {
$words[$position] = mb_strtoupper( $word );
/* capitalize first letter of all other words, if... */
} elseif (
/* ...first word of the title string... */
0 === $position ||
/* ...or not in above lower-case list*/
! in_array( $word, $articles_conjunctions_prepositions )
) {
$words[$position] = ucwords( $word );
}
}
/* re-combine word array */
$string = implode( \' \', $words );
/* return title string in title case */
return $string;
}
显然,这两个单词列表都可以扩展——小写列表尤其是更多的介词,首字母缩略词则是当前网站上经常使用的词。
无论如何,特定于WP的部分只是上面的代码块。