Wordpress ..更改网址图片后,图片仍然有以前的永久链接

时间:2015-11-17 20:19:05

标签: wordpress image permalinks

我用define('UPLOADS'等)更改了url图像。这行得通。我一直在改变手动图像网址。但是wordpress仍然在旧图像上有聋道/ wp-content / upload。只有新图像才能正确显示。 (我不能重新上传所有图像!!)我怎么办? 感谢

1 个答案:

答案 0 :(得分:0)

图像的URL存储在主机名完全限定域的前面。如果您只需要替换旧帖子的内容,则可以遍历数据库中的所有帖子,然后将旧URL替换为新URL,然后使用wp_update_post()将帖子更新到数据库。 / p>

// get all published posts
$args = array(
    'post_status' => 'publish',
    'posts_per_page' => -1
);

$posts = new WP_Query( $args );

// the full old URL, for replacing
$old_upload_baseurl = 'http://www.example.com/wp-content/uploads';

// get the new upload info
$upload_dir = wp_upload_dir();

// loop over the posts and update the content
if ( $posts->have_posts() ): while ( $posts->have_posts() ): $posts->the_post();
    // get the current (unfiltered) content. 
    // don't use get_the_content() as it will filter the response.
    $post_content = $post->post_content;

    // replace the old url with the new url
    $post_content = preg_replace( $old_upload_baseurl, $upload_dir['baseurl'], $post_content );

    // update the post content
    $post_data = array(
        'ID' => $post->ID,
        'post_content' => $post_content
    );
    wp_update_post( $post_data );
endwhile; endif;