@cybnet 答案很好(我投了更高的票),而且比我的简单。
当他编辑他的答案时,添加了类似于模板标签的功能,我已经写了这个标签。。我决定不删除这个答案,因为我使用了不同的方法。
(内联注释中代码后的解释)
add_action(\'pre_post_update\', \'separate_content_images\', 99, 2)
add_filter( \'the_content\', \'separate_content_images\', 99, 1 );
function separate_content_images( $content = \'\', $data = -1, $return_img = false ) {
  if ( empty($content) || is_object($content) ) { // function called directly inside loop
    if ( empty($content) ) global $post;
    if ( object($content) ) $post = $content;
    $content = is_object($post) && isset($post->post_content) ? $post->post_content : false;
  }
  if ( ! $content ) return;
  if ( is_int($content) && is_array($data) ) { // function triggered on post update
    $update = $content;
    $content = $data[\'post_content\'];
  }
  $imgs = preg_match_all("/<img[^>]+\\>/i", $content, $matches);
  // no images, return content when not in update, null otherwise
  if ( empty($matches) ) return $update ? null : $content;
  $post_images = array();
  foreach ($matches as $img_match ) {
    // save an array of all images in content
    $post_images[] = $img_match[0];
    // remove images from content
    $content = str_replace($img_match[0], \'\', $content); 
  }
  if ( $update ) {
    // update the post content preventing infinite loop by removing action
    remove_action(\'pre_post_update\', \'separate_content_images\', 99, 2);
    wp_update_post( array(\'ID\'=> $update, \'post_content\'=> $content) );
    add_action(\'pre_post_update\', \'separate_content_images\', 99, 2)
    // save all images in a custom field
    update_post_meta($update , \'_post_images\', $post_images);
  } else {
    return $return_img ? $post_images : $content;
  }
}
 此函数挂接在更新前操作和内容过滤器中。
因此,当更新包含图像的帖子时,图像将从内容中剥离出来,并自动放入自定义字段。内容也会更新为cntain no图像。
如果使用显示包含图像的帖子the_content, 打印的是没有图像的内容。
但如何打印剥离的图像呢?为以下目的编写自定义模板标记:
print_post_images( $args = array(), $post = null ) {
  $defaults = array(
    \'wrap\' => \'div\',
    \'wrap_class\' => \'\',
    \'before\' => \'\',
    \'after\' => \'<br />\',
    \'link_to\' => \'self\' // self img link to file, \'post\' to post, other values no link,
    \'before_link\' => \'\',
    \'after_link\' => \'\',
    \'a_class\' => \'\',
    \'target\' => \'\',
    \'echo\' => true
  );
  if ( empty($post) ) global $post;
  if ( is_int($post) ) $post = get_post($post);
  if ( empty($post) || ! isset($post->post_content) ) return;
  $args = extract( wp_parse_args($args, $defaults) );
  // try to get images from custom field (for updated posts)
  $images = get_post_meta($post->ID, \'_post_images\', true);
  if ( empty($images) ) { // no images in custom field, try on content
    $images = separate_content_images( $post, -1, true );
  }
  if ( ! empty($images) ) {
    $out = \'\';
    if ($wrap) {
      $format = $wrap_class ? \'<%s class="%s">\' : \'<%s>\';
      $out .= sprintf($format, $wrap, esc_attr($wrap_class) );
    }
    foreach ( $images as $image ) {
      if ($link_to == \'post\' || $link_to == \'self\' ) {
        $format = $a_class ? \'<a href="%s" class="%s">\' : \'<a href="%s">\';
        if ( $target ) $format = str_replace(\'>\', \' target="%s">\', $format);
        if ($link_to == \'post\') {
           $url = get_permalink($post);
        } else {
           preg_match(\'/src=["\']([^"\']+)["\']/\', $image, $matches);
           $url = isset($matches[1]) ? $matches[1] : false;
        } 
        if ( ! $url ) continue;
        if ($before_link) $out .= $before_link;
        $out .= sprintf($format, esc_url($url), esc_attr($a_class), esc_attr($target) );
      }
      if ($before) $out .= $before;
      $out .= $image;
      if ($after) $out .= $after;
      if ($link_to == \'post\' || $link_to == \'self\' ) {
        $out .= \'</a>\';
        if ($after_link) $out .= $after_link;
      }
    }
    if ($wrap) $out .= sprintf("</%s>", $wrap);
    // enable a filter to customize the output
    $out = apply_filters(\'print_post_images\', $out, $post, $args);   
    if ($echo) echo $out; else return $out;
  }
}
 现在,在您的循环中,无论您想在哪里添加
<?php print_post_images(); ?>
 打印所有图像。您可以使用提供的参数自定义大量输出。此外,您可以使用自定义过滤器自定义输出
\'print_post_images\'. 
此函数可在循环外部使用,使用传递post ID或post对象作为第二个参数:
<?php print_post_images( $args, $post ); ?>
 若您只想返回一组图像,请使用
<?php $images = separate_content_images( $post, -1, true ); ?>
 在哪里
$post 可以是post ID或post对象。