我想在所有内容帖子的开头添加一个句子,直接进入post_content WordPress数据库。我该怎么做?可以通过function.php或MySQL命令。
发布于 2018-11-18 18:47:26
我会选择@butlerblog发布的解决方案--它在未来更容易管理和改变,而且它更好的实践。
但是..。如果你问..。以下是您可以这样做的方法:
function prepend_content_to_posts() {
$to_prepend = 'This will be prepended';
$posts = get_posts( array( 'posts_per_page' => -1 ) );
foreach ( $posts as $post ) {
wp_update_post( array(
'ID' => $post->ID,
'post_content' => $to_prepend . $post->post_content
) );
}
}那你就得调用这个函数了。小心不要叫它两次-在这种情况下,它会添加你的内容两次。
UPDATE wp_posts
SET post_content = CONCAT('', post_content)
WHERE post_type = 'post' AND post_status = 'publish'请注意根据配置更改表前缀。
发布于 2018-11-16 19:18:31
与其直接将其插入db中,不如在使用the_content筛选器显示时将其添加到内容中?这样,您以后就不会进行任何数据库更改。
add_filter( 'the_content', 'my_extra_line' );
function my_extra_line( $content ) {
if ( "post" == get_post_type() ) {
// If the content is a post, add an extra line to the content.
$content = "My extra line of content. " . $content;
}
return $content;
}https://wordpress.stackexchange.com/questions/319448
复制相似问题