<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>文章附件相关文章列表 | 皇家元林</title>
	<atom:link href="https://hjyl.org/tags/%E6%96%87%E7%AB%A0%E9%99%84%E4%BB%B6/feed/" rel="self" type="application/rss+xml" />
	<link>https://hjyl.org</link>
	<description>刘元林的个人博客</description>
	<lastBuildDate>Sat, 07 Dec 2024 04:49:54 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://img.hjyl.org/uploads/2019/10/cropped-about-me-32x32.png</url>
	<title>文章附件相关文章列表 | 皇家元林</title>
	<link>https://hjyl.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>wp_get_attachment_image_src获取不到文章附件？</title>
		<link>https://hjyl.org/wp-get-attachment-image-src-get-photos/</link>
					<comments>https://hjyl.org/wp-get-attachment-image-src-get-photos/#comments</comments>
		
		<dc:creator><![CDATA[皇家元林]]></dc:creator>
		<pubDate>Sat, 07 Dec 2024 04:47:56 +0000</pubDate>
				<category><![CDATA[元林手札]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[wp_get_attachment_image_src]]></category>
		<category><![CDATA[图片]]></category>
		<category><![CDATA[媒体库]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[文章附件]]></category>
		<category><![CDATA[相册]]></category>
		<guid isPermaLink="false">https://hjyl.org/?p=5572</guid>

					<description><![CDATA[今天无意打开博客相册，发现最近文章里的图片没有获取到，仅是最近一篇文章里的，所以我猜是那篇文章的问题。我依稀记 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>今天无意打开博客<a href="https://hjyl.org/photos/">相册</a>，发现最近文章里的图片没有获取到，仅是最近一篇文章里的，所以我猜是那篇文章的问题。我依稀记得当时是先上传图片到媒体库，然后插入到文章里的。难道这还有区别？果然，我发现了不一样。</p>



<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1408" height="726" data-id="5574" src="https://img.hjyl.org/uploads/2024/12/wechat_2024-12-07_094004_287.png"  class="wp-image-5574" title="wechat_2024 12 07_094004_287.png" alt="wechat_2024 12 07_094004_287.png" /></figure>



<figure class="wp-block-image size-large"><img decoding="async" width="1412" height="714" data-id="5573" src="https://img.hjyl.org/uploads/2024/12/wechat_2024-12-07_093912_340.png"  class="wp-image-5573" title="wechat_2024 12 07_093912_340.png" alt="wechat_2024 12 07_093912_340.png" /></figure>
</figure>



<p>这就过分了。所以说需要数据库支持的相册就是很麻烦。我不能一个个删掉再重新上传吧。那只能修改数据库咯。</p>



<h3 class="wp-block-heading">查找数据库</h3>



<p>媒体和文章一样，都在wp_posts表里，sql查询可得：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers">Select * from wp_posts where post_type = 'attachment';</code></pre>



<p>最多显示500条，你可以导出来看，我就是这么干的，然后用文本编辑器打开都能看。</p>



<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large"><img decoding="async" width="866" height="498" data-id="5577" src="https://img.hjyl.org/uploads/2024/12/wechat_2024-12-07_094047_352.png"  class="wp-image-5577" title="wechat_2024 12 07_094047_352.png" alt="wechat_2024 12 07_094047_352.png" /><figcaption class="wp-element-caption">mysql数据库查询结果</figcaption></figure>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1055" height="163" data-id="5576" src="https://img.hjyl.org/uploads/2024/12/wechat_2024-12-07_094155_048.png"  class="wp-image-5576" title="wechat_2024 12 07_094155_048.png" alt="wechat_2024 12 07_094155_048.png" /><figcaption class="wp-element-caption">导出来之后在文本编辑器下</figcaption></figure>
</figure>



<p>经过对比，可以看出在post_parent字段，就是显示图片插到哪篇文章ID下，如果显示为0，那说明只是在媒体库里，不在文章中显示，这样wp_get_attachment_image_src也就获取不了。</p>



<h3 class="wp-block-heading">修改数据库</h3>



<p>知道原因了就好修改了，一种方法是直接在phpmyadmin下用上面的查询代码找出来，一个个修改；一种可以用sql语句批量修改。但修改前，必须得备份数据库，数据无价。</p>



<p>sql语句：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers">UPDATE `wp_posts` SET `post_parent` = '文章ID' WHERE `wp_posts`.`ID` BETWEEN 文章ID起始 AND 文章ID结束 AND `wp_posts`.`post_parent` = 0;</code></pre>



<h3 class="wp-block-heading">其他问题</h3>



<p>但是改了之后，相册是正常了，wp_get_attachment_image_src函数也能获取了，但是查看媒体库里的照片时，还是没有显示上传至哪篇文章下，如第一张图片。</p>



<p>另外在wp_postmeta表里找到相应的缩略图信息，可以用sql筛选一下：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"> SELECT * FROM  `wp_postmeta` WHERE meta_key IN ('_wp_attached_file', '_wp_attachment_backup_sizes',  '_wp_attachment_metadata',  '_thumbnail_id')</code></pre>



<p>貌似没找到相应的设置字段。知道的童鞋麻烦告知一下，不过到此问题是解决了，虽然不是很完美。通过sql修改数据，不是经常干，稳妥一些，切记一定要先备份数据库，切记！！！</p>



<p>参考文章：</p>



<p>①https://cloud.tencent.com/developer/information/mysql%E4%BF%AE%E6%94%B9%E5%AD%97%E6%AE%B5%E5%80%BC</p>



<p>②https://doc.weixiaoduo.com/wordpress/4468.html</p>
<div id="content-copyright"><span style="font-weight:bold;text-shadow:0 1px 0 #ddd;font-size: 13px;">版权声明: </span><span style="font-size: 13px;">本文采用 <a href="https://hjyl.org/go/aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzMuMC8=" rel="nofollow" target="_blank">BY-NC-SA</a> 协议进行授权，如无注明均为原创，转载请注明转自 <a href="https://hjyl.org">皇家元林</a><br>本文链接: <a rel="bookmark" title="wp_get_attachment_image_src获取不到文章附件？" href="https://hjyl.org/wp-get-attachment-image-src-get-photos/">wp_get_attachment_image_src获取不到文章附件？</a></span></div>]]></content:encoded>
					
					<wfw:commentRss>https://hjyl.org/wp-get-attachment-image-src-get-photos/feed/</wfw:commentRss>
			<slash:comments>24</slash:comments>
		
		
			</item>
	</channel>
</rss>
