admin管理员组文章数量:1306820
I imported over 1000 posts from a Tumblr gallery. All the posts are untitled, but each has a photo and name inside of the post. How do I make take the content of each post and make it the post name?
I imported over 1000 posts from a Tumblr gallery. All the posts are untitled, but each has a photo and name inside of the post. How do I make take the content of each post and make it the post name?
Share Improve this question edited Jan 8, 2013 at 3:06 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jan 8, 2013 at 2:23 CecilyCecily 771 silver badge6 bronze badges1 Answer
Reset to default 5Hook into the_post
– called when the post is actually used – and fill the title. Be aware the slug has to be changed too.
If you are used not to enter a title, hook into save_post
too, and let the same code do this for you.
The code
Download on GitHub
<?php
/**
* Plugin Name: T5 Lazy Title Updates
* Description: Fill missing post titles from content when a post is called
* Plugin URI:
* Version: 2013.01.08.02
* Author: Fuxia Scholz
* Licence: MIT
* License URI: http://opensource/licenses/MIT
*/
add_action( 'the_post', 't5_lazy_title_update' );
add_action( 'save_post', 't5_lazy_title_update_save', 10, 2 );
/**
* Add atitle when the post is called in setup_postdata().
*
* @wp-hook the_post
* @param object $post
* @return void
*/
function t5_lazy_title_update( $post )
{
if ( ! empty ( $post->post_title )
and strip_shortcodes( $post->post_title ) === $post->post_title
)
return;
$clean_content = wp_strip_all_tags( $post->post_content, TRUE );
$clean_content = strip_shortcodes( $clean_content );
if ( '' === $clean_content )
return;
$words = preg_split( "/\s+/", $clean_content, 40, PREG_SPLIT_NO_EMPTY );
$title = implode( ' ', $words );
$title = rtrim( $title, '.;,*' );
$slug = wp_unique_post_slug(
sanitize_title_with_dashes( $title ),
$post->ID,
$post->post_status,
$post->post_type,
$post->post_parent
);
wp_update_post(
array (
'ID' => $post->ID,
'post_title' => $title,
'post_name' => $slug
)
);
// $post is passed by reference, so we update this property in realtime
$post->post_title = $title;
$post->post_name = $slug;
}
/**
* Fill title from post content on save
*
* @wp-hook save_post
* @param int $post_ID
* @param object $post
* @return void;
*/
function t5_lazy_title_update_save( $post_ID, $post )
{
t5_lazy_title_update( $post );
}
本文标签: bulkFill post titles from post content
版权声明:本文标题:bulk - Fill post titles from post content? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741826156a2399653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论