Wordpress:将内嵌图像移动到精选图库

时间:2014-08-01 00:28:38

标签: php wordpress plugins

这是我的情况:

我正在重新设计现有的Wordpress网站。

新设计将所有图片与实际帖子内容分开,并将其放入Featured Gallery

目前,每篇文章都会在帖子内容中显示典型的内联图片。

这甚至可以从帖子内容中提取所有内嵌图像,并为每个帖子创建包含这些图像的精选画廊吗?

在过去,我已经做了类似下面的事情来抓住帖子内容中的第一张图片,并将其设置为标准特色图片,但没有像我在这个困境中所做的那样。

function wpforce_featured() {
      global $post;
      $already_has_thumb = has_post_thumbnail($post->ID);
          if (!$already_has_thumb)  {
          $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                      if ($attached_image) {
                            foreach ($attached_image as $attachment_id => $attachment) {
                            set_post_thumbnail($post->ID, $attachment_id);
                            }
                       }
                    }
  }  //end function
  add_action('the_post', 'wpforce_featured');
  add_action('save_post', 'wpforce_featured');
  add_action('draft_to_publish', 'wpforce_featured');
  add_action('new_to_publish', 'wpforce_featured');
  add_action('pending_to_publish', 'wpforce_featured');
  add_action('future_to_publish', 'wpforce_featured');

2 个答案:

答案 0 :(得分:1)

基本上我使用的方法是创建一个我用来将所有内嵌图像从帖子移动到精选图库的脚本,如下所示:

// Loop over every post

while ( have_posts() ) : the_post();    

    // Get the images attached to the post.

    $imageids = array();

    $images = get_attached_media( 'image', $post->ID );

    foreach ($images as $image) {
        $imageids[] = $image->ID;
    }

    $comma_separated = implode(",", $imageids);

    // Save them to the Featured Gallery (Got this info by digging into the Featured Gallery plugin's source code)

    if ( $post->post_type == 'revision' ) {return;}
    if ( get_post_meta( $post->ID, 'fg_perm_metadata', FALSE ) ) {
        update_post_meta( $post->ID, 'fg_perm_metadata', $comma_separated );
    } else {
        add_post_meta( $post->ID, 'fg_perm_metadata', $comma_separated );
    }
    if ( !$comma_separated ) delete_post_meta( $post->ID, 'fg_perm_metadata' );

endwhile;

// Reset Query

wp_reset_query();

然后,我创建了一个删除帖子内容中所有图像的函数,我将它放在functions.php中:

function remove_images( $content ) 
{
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
                '{ wp-image-[0-9]*" /></a>}'),
            array('<img','" />'),
            $content
        );
    $content = preg_replace('/<img(.*)>/i','', $content, 1);
    return $content;
}
add_filter( 'the_content', 'remove_images' );

首先preg_replace删除图片周围的链接。第二个删除图像。

答案 1 :(得分:0)

问题是,如果我正确地读到你的是所有图片都被定位为帖子内容中的网址?

你可以解析它们:

$content = get_the_content();
$html = new DOMDocument;
$html->loadHTML($content);

//get the images
$images = $html->getElementsByTagName('img');

foreach($images as $image=>$key {
     $imageurls[]=$key->attributes->getNamedItem("src")->value;//play with this cant remember how it returns object.
}

$content= preg_replace('/<img(.*)>/i','',$content,1);

echo $content; 

// you also have a array of images to use.