<?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>运达&#039;s  blog &#187; 相关文章</title>
	<atom:link href="https://www.yunda51.com/?feed=rss2&#038;tag=%E7%9B%B8%E5%85%B3%E6%96%87%E7%AB%A0" rel="self" type="application/rss+xml" />
	<link>https://www.yunda51.com</link>
	<description>运达的博客</description>
	<lastBuildDate>Wed, 12 Nov 2025 07:58:26 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.19</generator>
	<item>
		<title>WordPress添加相关文章功能（标题/缩略图样式）</title>
		<link>https://www.yunda51.com/?p=1661</link>
		<comments>https://www.yunda51.com/?p=1661#comments</comments>
		<pubDate>Thu, 31 Dec 2015 02:15:59 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[相关文章]]></category>
		<category><![CDATA[缩略图]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1661</guid>
		<description><![CDATA[直接上代码： 1.添加含缩略图的相关文章 如图： 首先1)在主题的 functions.php 的最后一个 ?<a href="https://www.yunda51.com/?p=1661" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>直接上代码：<br />
<strong>1.添加含缩略图的相关文章</strong><br />
如图：<br />
<a href="http://www.yunda51.com/wp-content/uploads/2015/12/109.png"><img class="alignnone size-full wp-image-1662" src="http://www.yunda51.com/wp-content/uploads/2015/12/109.png" alt="109" width="578" height="200" /></a><br />
<strong>首先1)在主题的 functions.php 的最后一个 ?> 前添加下面的代码：</strong></p>
<pre class="wp-code-highlight prettyprint">
    //添加特色缩略图支持

    if ( function_exists(&#039;add_theme_support&#039;) )add_theme_support(&#039;post-thumbnails&#039;);

     

    //输出缩略图地址 From wpdaxue.com

    function post_thumbnail_src(){
        global $post;
    	if( $values = get_post_custom_values(&quot;thumb&quot;) ) {	//输出自定义域图片地址
    		$values = get_post_custom_values(&quot;thumb&quot;);
    		$post_thumbnail_src = $values [0];
    	} elseif( has_post_thumbnail() ){    //如果有特色缩略图，则输出缩略图地址
            $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post-&gt;ID),&#039;full&#039;);
    		$post_thumbnail_src = $thumbnail_src [0];
        } else {
    		$post_thumbnail_src = &#039;&#039;;
    		ob_start();
    		ob_end_clean();
    		$output = preg_match_all(&#039;/&lt;img.+src=[\&#039;&quot;]([^\&#039;&quot;]+)[\&#039;&quot;].*&gt;/i&#039;, $post-&gt;post_content, $matches);
    		$post_thumbnail_src = $matches [1] [0];   //获取该图片 src
    		if(empty($post_thumbnail_src)){	//如果日志中没有图片，则显示随机图片
    			$random = mt_rand(1, 10);
    			echo get_bloginfo(&#039;template_url&#039;);
    			echo &#039;/images/pic/&#039;.$random.&#039;.jpg&#039;;
    			//如果日志中没有图片，则显示默认图片
    			//echo &#039;/images/default_thumb.jpg&#039;;
    		}
    	};
    	echo $post_thumbnail_src;
    }
</pre>
<p>PS：上面的代码主要是获取图片链接，获取的顺序是：</p>
<p>自定义字段为 thumb 的图片>特色缩略图>文章第一张图片>随机图片/默认图片；</p>
<p>随机图片：请制作10张图片，放在现用主题文件夹下的 images/pic/ 目录，图片为jpg格式，并且使用数字 1-10命名，比如 1.jpg；如果你不想用随机图片，请将 倒数第5行 前面的“//”去掉，然后给 倒数第7、9行 前面添加“//”注销，并且在现用主题的 /images/ 目录下添加一张名字为 default_thumb.jpg 的默认图片，这样，就会显示默认图片。</p>
<p><strong>2)将下面的代码添加到 single.php 要显示相关文章的位置：</strong></p>
<pre class="wp-code-highlight prettyprint">
    &lt;h3&gt;相关文章&lt;/h3&gt;

    &lt;ul class=&quot;related_img&quot;&gt;

    &lt;?php
    $post_num = 4;
    $exclude_id = $post-&gt;ID;
    $posttags = get_the_tags(); $i = 0;
    if ( $posttags ) {
    	$tags = &#039;&#039;; foreach ( $posttags as $tag ) $tags .= $tag-&gt;term_id . &#039;,&#039;;
    	$args = array(
    		&#039;post_status&#039; =&gt; &#039;publish&#039;,
    		&#039;tag__in&#039; =&gt; explode(&#039;,&#039;, $tags),
    		&#039;post__not_in&#039; =&gt; explode(&#039;,&#039;, $exclude_id),
    		&#039;caller_get_posts&#039; =&gt; 1,
    		&#039;orderby&#039; =&gt; &#039;comment_date&#039;,
    		&#039;posts_per_page&#039; =&gt; $post_num
    	);

    	query_posts($args);
    	while( have_posts() ) { the_post(); ?&gt;
    		&lt;li class=&quot;related_box&quot;  &gt;
    		&lt;div class=&quot;r_pic&quot;&gt;
    		&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot; target=&quot;_blank&quot;&gt;
    		&lt;img src=&quot;&lt;?php echo post_thumbnail_src(); ?&gt;&quot; alt=&quot;&lt;?php the_title(); ?&gt;&quot; class=&quot;thumbnail&quot; /&gt;
    		&lt;/a&gt;
    		&lt;/div&gt;
    		&lt;div class=&quot;r_title&quot;&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot; target=&quot;_blank&quot; rel=&quot;bookmark&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/div&gt;
    		&lt;/li&gt;
    	&lt;?php
    		$exclude_id .= &#039;,&#039; . $post-&gt;ID; $i ++;
    	} wp_reset_query();
    }

    if ( $i &lt; $post_num ) {

    	$cats = &#039;&#039;; foreach ( get_the_category() as $cat ) $cats .= $cat-&gt;cat_ID . &#039;,&#039;;
    	$args = array(
    		&#039;category__in&#039; =&gt; explode(&#039;,&#039;, $cats),
    		&#039;post__not_in&#039; =&gt; explode(&#039;,&#039;, $exclude_id),
    		&#039;caller_get_posts&#039; =&gt; 1,
    		&#039;orderby&#039; =&gt; &#039;comment_date&#039;,
    		&#039;posts_per_page&#039; =&gt; $post_num - $i
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post(); ?&gt;
    	&lt;li class=&quot;related_box&quot;  &gt;
    		&lt;div class=&quot;r_pic&quot;&gt;
    		&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot; target=&quot;_blank&quot;&gt;
    		&lt;img src=&quot;&lt;?php echo post_thumbnail_src(); ?&gt;&quot; alt=&quot;&lt;?php the_title(); ?&gt;&quot; class=&quot;thumbnail&quot; /&gt;
    		&lt;/a&gt;
    		&lt;/div&gt;
    		&lt;div class=&quot;r_title&quot;&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot; target=&quot;_blank&quot; rel=&quot;bookmark&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/div&gt;
    	&lt;/li&gt;
    	&lt;?php $i++;
    	} wp_reset_query();
    }
    if ( $i  == 0 )  echo &#039;&lt;div class=&quot;r_title&quot;&gt;没有相关文章!&lt;/div&gt;&#039;;
    ?&gt;
    &lt;/ul&gt;
</pre>
<p>PS：第四行$post_num = 4; 表示调用4篇文章，请根据自己需要修改。</p>
<p>css样式自己写，也可参考一下：</p>
<pre class="wp-code-highlight prettyprint">
    .related_posts{margin-top:5px;}
    .related_img{width:600px;height:210px;}
    .related_box{float:left;overflow:hidden;margin-top:5px;width:148px;border-right:1px #eee solid}
    .related_box:hover{background:#f9f9f9}
    .related_box .r_title{width:auto;height:72px;font-weight:400;font-size:14px;margin:0 10px;overflow:hidden;}
    .related_box .r_pic{margin:6px}
    .related_box .r_pic img{width:130px;height:100px;border:1px  solid #e1e1e1;background:#fff;padding:2px}
</pre>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1661</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
