<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>拾光 - WordPress所有图片</title>
<link>https://www.ishiguang.cn/tag/WordPress%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87/</link>
<atom:link href="https://www.ishiguang.cn/feed/tag/WordPress%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87/" rel="self" type="application/rss+xml" />
<language>zh-CN</language>
<description></description>
<lastBuildDate>Sat, 26 Feb 2022 17:19:00 +0800</lastBuildDate>
<pubDate>Sat, 26 Feb 2022 17:19:00 +0800</pubDate>
<item>
<title>WordPress获取网站文章中所有图片代码方法</title>
<link>https://www.ishiguang.cn/15411.html</link>
<guid>https://www.ishiguang.cn/15411.html</guid>
<pubDate>Sat, 26 Feb 2022 17:19:00 +0800</pubDate>
<dc:creator>ssr</dc:creator>
<description><![CDATA[如果我们需要调用WordPress网站文章中的图片，我们可以通过直接获取媒体图片的方式，当然也是有插件可以调用的。在这篇文章中，老蒋需要分享的是无插件可以实现调用WP程序中所有文章的图片。这个其...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>如果我们需要调用WordPress网站文章中的图片，我们可以通过直接获取媒体图片的方式，当然也是有插件可以调用的。在这篇文章中，老蒋需要分享的是无插件可以实现调用WP程序中所有文章的图片。这个其实对于有些图片类型的网站是有用的，我们需要展示所有的图片。 </p><p>第一、脚本部分</p><pre><code>    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;);
              } 
</code></pre><p>将代码加入到当前主题的Functions.php文件中。</p><p>第二、如何调用</p><pre><code>   &lt;?php echo hui_get_thumbnail(false,true);?&gt; 
</code></pre><p>在我们需要调用的主题单页面或者是某个页面直接加入代码就可以调出图片。</p>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://www.ishiguang.cn/15411.html#comments</comments>
<wfw:commentRss>https://www.ishiguang.cn/feed/tag/WordPress%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87/</wfw:commentRss>
</item>
</channel>
</rss>