【问题标题】:Wordpress: Moving inline images to Featured GalleriesWordpress:将内联图像移动到特色画廊
【发布时间】:2014-08-01 00:28:37
【问题描述】:

这是我的情况:

我正在重新设计现有的 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');

【问题讨论】:

    标签: php wordpress plugins


    【解决方案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 删除图像周围的链接。第二次删除图像。

    【讨论】:

    • 所以我在一年半之后才对此发表评论,我意识到。但是,刚刚发现这个,想说,作为Featured Galleries的作者,这个解决方案很棒。干杯。
    【解决方案2】:

    如果我没看错的话,问题是所有图片都在帖子内容中作为 url 定位吗?

    你可以解析它们:

    $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.
    

    【讨论】:

    • 所以我基本上必须构建一个自定义文件才能运行一次,并将这段代码放在一个循环中,循环遍历每个帖子。哦,添加创建精选图库并将其附加到帖子的实际代码。最后一部分是我最困惑的部分。
    • 你把这件事复杂化了。 id 建议将上述内容包装在 2 个函数中(1 个用于解析器获取图像并添加图库 html 和 js),第二个用于 preg 替换以从内容中删除图像,使其不会出现两次。然后不要在 single.php 中使用 the_content,而是使用您的自定义函数。除此之外,您可以挂钩到 pre-get-posts 并将 the_content 修改为完整的 html 字符串,包括您的画廊。
    猜你喜欢
    • 2015-05-25
    • 1970-01-01
    • 2018-04-06
    • 2012-01-23
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多