<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel rdf:about="https://www.ishiguang.cn/feed/rss/tag/WordPress%E5%9B%BE%E7%89%87/">
<title>拾光 - WordPress图片</title>
<link>https://www.ishiguang.cn/tag/WordPress%E5%9B%BE%E7%89%87/</link>
<description></description>
<items>
<rdf:Seq>
<rdf:li resource="https://www.ishiguang.cn/15388.html"/>
<rdf:li resource="https://www.ishiguang.cn/15411.html"/>
</rdf:Seq>
</items>
</channel>
<item rdf:about="https://www.ishiguang.cn/15388.html">
<title>删除WordPress文章时同时删除文章包含的图片</title>
<link>https://www.ishiguang.cn/15388.html</link>
<dc:date>2022-02-26T17:19:38+08:00</dc:date>
<description>有些时候我们已有编辑的文章可能不需要，然后删除文章的时候默认是不会同时删除文章中自带的图片以及附件的。这样势必会导致无用的图片占用服务器资源。有没有一个办法可以在删除文章的同时将这篇文章中包含的图片一并删除，这样可以节省空间。   / 删除文章时删除图片附件 / function delete_post_and_attachments($post_ID) {&lt;br&gt; global $wpdb; //删除特色图片 $thumbnails = $wpdb-&gt;get_results( "SELECT * FROM $wpdb-&gt;postmeta WHERE meta_key = &#039;_thumbnail_id&#039; AND post_id = $post_ID" ); foreach ( $thumbnails as $thumbnail ) { wp_delete_attachment( $thumbnail-&gt;meta_value, true );&lt;br&gt; }&lt;br&gt; //删除图片附件&lt;br&gt; $attachments = $wpdb-&gt;get_results( &quot;SELECT * FROM $wpdb-&gt;posts WHERE post_parent = $post_ID AND post_type = &#039;attachment&#039;&quot; );&lt;br&gt; foreach ( $attachments as $attachment ) {&lt;br&gt; wp_delete_attachment( $attachment-&gt;ID, true ); } $wpdb-&gt;query( &quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" ); } add_action('before_delete_post', 'delete_post_and_attachments');  如果需要使用的话，只需要将代码添加到当前主题的Functions.php文件中。</description>
</item>
<item rdf:about="https://www.ishiguang.cn/15411.html">
<title>WordPress获取网站文章中所有图片代码方法</title>
<link>https://www.ishiguang.cn/15411.html</link>
<dc:date>2022-02-26T17:19:00+08:00</dc:date>
<description>如果我们需要调用WordPress网站文章中的图片，我们可以通过直接获取媒体图片的方式，当然也是有插件可以调用的。在这篇文章中，老蒋需要分享的是无插件可以实现调用WP程序中所有文章的图片。这个其实对于有些图片类型的网站是有用的，我们需要展示所有的图片。 第一、脚本部分    function hui_get_thumbnail( $single=true, $must=true ) {
  global $post;
  $html = &#039;&#039;;
  if ( has_post_thumbnail() ) {
  $domsxe = simplexml_load_string(get_the_post_thumbnail());
  $src = $domsxe-&gt;attributes()-&gt;src;
  $src_array = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), &#039;thumbnail&#039;);
  $html = sprintf(&#039;&lt;li&gt;
&lt;img src=&quot;%s&quot; /&gt;
  &lt;/li&gt;&#039;, $src_array[0]);
  } else {
  $content = $post-&gt;post_content;
  preg_match_all(&#039;/&lt;img.*?(?: |\\t|\\r|\\n)?src=[\&#039;&quot;]?(.+?)[\&#039;&quot;]?(?:(?:
       |\\t|\\r|\\n)+.*?)?&gt;/sim&#039;, $content, $strResult, PREG_PATTERN_ORDER);
      $images = $strResult[1];
      $counter = count($strResult[1]);
      $i = 0;
      foreach($images as $src){
      $i++;
      $src2 = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), &#039;thumbnail&#039;);
      $src2 = $src2[0];
      if( !$src2 &amp;&amp; true ){
      $src = $src;
      }else{
      $src = $src2;
      }
      $item = sprintf(&#039;&lt;li&gt;
    &lt;img src=&quot;%s&quot; /&gt;
&lt;/li&gt;&#039;, $src);
      if( $single){
      return $item;
      break;
      }
      $html .= $item;
      if(
      ($counter &gt;= 4 &amp;&amp; $counter &lt; 8
  &amp;&amp;
  $i&gt;= 4) ||
          ($counter &gt;= 8 &amp;&amp; $i &gt;= 8) ||
          ($counter &gt; 0 &amp;&amp; $counter &lt; 4
      &amp;&amp;
      $i&gt;= $counter)
              ){
              break;
              }
              }
              }
              return $html;
              }
              function hui_get_attachment_id_from_src ($link) {
              global $wpdb;
              $link = preg_replace(&#039;/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i&#039;, &#039;&#039;, $link);
              return $wpdb-&gt;get_var(&quot;SELECT ID FROM {$wpdb-&gt;posts} WHERE guid=&#039;$link&#039;&quot;);
              } 
将代码加入到当前主题的Functions.php文件中。第二、如何调用   &lt;?php echo hui_get_thumbnail(false,true);?&gt; 
在我们需要调用的主题单页面或者是某个页面直接加入代码就可以调出图片。</description>
</item>
</rdf:RDF>