讓好想法,成為好網站

WordPress添加相關文章的幾種方法

本文圍繞“WordPress添加相關文章的幾種方法”整理操作說明與配置思路。界面名稱可能隨軟件版本調整,修改線上網站前請保存備份。涉及使用體驗的描述屬於來源作者,本頁未提供獨立實測結論。 給WordPress添加相關文章代碼,對於網站的體驗和SEO來說都有好處,本文就分享一下WordPress相關文

更新於 2026-11-24編輯整理

BUILD WITH CONFIDENCE

讓想法繼續前進

瀏覽主題模板

基本資訊

資料範圍
完整文章正文,已清理外鏈和來源推廣
正文插圖
2個真實URL;2張已本地化,其餘使用原圖待補

本文圍繞“WordPress添加相關文章的幾種方法”整理操作說明與配置思路。界面名稱可能隨軟件版本調整,修改線上網站前請保存備份。涉及使用體驗的描述屬於來源作者,本頁未提供獨立實測結論。

給WordPress添加相關文章代碼,對於網站的體驗和SEO來說都有好處,本文就分享一下WordPress相關文章的代碼實現方式,新手的話建議使用主題自帶功能或者插件實現。

代碼版方法一

修改主題文件前記得先備份,有問題再還原。

1、在主題函數文件functions.php裏面添加下面的內容

/**
 * Related posts
 * 
 * @global object $post
 * @param array $args
 * @return
 */
function wcr_related_posts($args = array()) {
    global $post;

    // default args
    $args = wp_parse_args($args, array(
        'post_id' => !empty($post) ? $post->ID : '',
        'taxonomy' => 'category',
        'limit' => 3,
        'post_type' => !empty($post) ? $post->post_type : 'post',
        'orderby' => 'date',
        'order' => 'DESC'
    ));

    // check taxonomy
    if (!taxonomy_exists($args['taxonomy'])) {
        return;
    }

    // post taxonomies
    $taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids'));

    if (empty($taxonomies)) {
        return;
    }

    // query
    $related_posts = get_posts(array(
        'post__not_in' => (array) $args['post_id'],
        'post_type' => $args['post_type'],
        'tax_query' => array(
            array(
                'taxonomy' => $args['taxonomy'],
                'field' => 'term_id',
                'terms' => $taxonomies
            ),
        ),
        'posts_per_page' => $args['limit'],
        'orderby' => $args['orderby'],
        'order' => $args['order']
    ));

    include( locate_template('related-posts-template.php', false, false) );

    wp_reset_postdata();
}

2、新建一個related-posts-template.php文件,添加以下內容:

<?php if (!empty($related_posts)) { ?>
    <div class="related-posts">
        <h3 class="widget-title"><?php _e('相關文章', ''); ?></h3>

        <ul class="related-posts-list">
            <?php
            foreach ($related_posts as $post) {
                setup_postdata($post);
            ?>
            <li>
                <a class="title" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php if (has_post_thumbnail()) { ?>
                    <div class="thumb">
                        <?php echo get_the_post_thumbnail(null, 'medium', array('alt' => the_title_attribute(array('echo' => false)))); ?>
                    </div>
                    <?php } ?>
                    <h4><?php the_title(); ?></h4>
                </a>
            </li>
            <?php } ?>
        </ul>
        <div class="clearfix"></div>
    </div>
<?php
}
相關文章

方法一高級調用方法

如果要調用相關文章代碼,只需要在你主題合適的位置,通常是添加到single.php文件裏面。

1、顯示3個相關文章內容代碼

<?php wcr_related_posts(); ?>

這種相關文章是根據分類來展示的。

2、根據文章TAG來展示相關文章

<?php 
wcr_related_posts(array(
   'taxonomy' => 'post_tag',
   'limit' => 6
));
?>

數字6自行修改,爲展示文章數量。

3、根據分類的熱門評論文章展示

<?php 
wcr_related_posts(array(
   'limit' => 6,
   'orderby' => 'comment_count',
   'order' => 'ASC'
));
?>

4、根據特定文章的TAG展示相關文章

<?php
wcr_related_posts(array(
   'limit' => 6,
   'taxonomy' => 'post_tag',
   'post_id' => 145
));
?>

最後,你可能還需要自己修改下css樣式讓相關文章更好看。

代碼版方法二

這種方法相對來說簡單點,調用特色圖的相關文章,如果你不是每篇文章都有特色圖,最好自己添加一張,或者安裝WordPress隨機顯示特色圖片插件:Random Post Thumbnails

WordPress相關文章

在你主題文章頁面合適的位置添加下面這段代碼

<div class="relatedposts">
<h3>相關文章</h3>
<?php
  $orig_post = $post;
  global $post;
  $tags = wp_get_post_tags($post->ID);
   
  if ($tags) {
  $tag_ids = array();
  foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  $args=array(
  'tag__in' => $tag_ids,
  'post__not_in' => array($post->ID),
  'posts_per_page'=>4, // 相關文章顯示數量.
  'caller_get_posts'=>1
  );
   
  $my_query = new wp_query( $args );
 
  while( $my_query->have_posts() ) {
  $my_query->the_post();
  ?>
   
  <div class="relatedthumb">
    <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
    <?php the_title(); ?>
    </a>
  </div>
   
  <? }
  }
  $post = $orig_post;
  wp_reset_query();
  ?>
</div>

上面代碼中the_post_thumbnail(array(150,100)的150,100是控制縮略圖大小的。

.relatedposts {width: 640px; margin: 0 0 20px 0; float: left; font-size: 12px;}
.relatedposts h3 {font-size: 20px; margin: 0 0 5px 0; }
.relatedthumb {margin: 0 1px 0 1px; float: left; }
.relatedthumb img {margin: 0 0 3px 0; padding: 0;}
.relatedthumb a {color :#333; text-decoration: none; display:block; padding: 4px; width: 150px;}
.relatedthumb a:hover {background-color: #ddd; color: #000;}

CSS文件,width: 640px是寬度。

使用這種代碼的話,最好在WP後臺的設置-媒體處把縮略圖大小設置爲一樣的。

代碼版方法三

這個代碼是展示同分類的其他文章

<?php
		// 默認參數
		$args = array(
			'posts_per_page' => 4, // 要顯示的項目數
			'post__not_in'   => array( get_the_ID() ), // 排除當前帖子
			'no_found_rows'  => true, 
		);

		// 檢查當前的帖子類別,並將tax_query添加到查詢參數中
		$cats = wp_get_post_terms( get_the_ID(), 'category' ); 
		$cats_ids = array();  
		foreach( $cats as $wpex_related_cat ) {
			$cats_ids[] = $wpex_related_cat->term_id; 
		}
		if ( ! empty( $cats_ids ) ) {
			$args['category__in'] = $cats_ids;
		}

		// 查詢文章
		$wpex_query = new wp_query( $args );

		// 輸出文章
		foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
		<div id="related_posts">
			<ul class="live">
				<li><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a></li>
			</ul>
		</div>
		<?php
		// 結束循環
		endforeach;
		wp_reset_postdata(); ?>

使用此代碼需要自定義.live的樣式,下面是來源作者目前使用的樣式

.live {
overflow: hidden;
margin: 0!important;
}
.live a{
text-decoration: none;
color: #32373c;
}
.live a:hover {
color: #009cee;
}
.live > li {
list-style: none;
position: relative;
padding: 0 0 0 2em;
margin: 0 0 .5em 10px;
-webkit-transition: .12s;
transition: .12s;
}
.live > li::before {
position: absolute;
content: '\2022';
font-family: Arial;
color: #111;
top: 0;
left: 0;
text-align: center;
font-size: 2em;
opacity: .5;
line-height: .75;
-webkit-transition: .5s;
transition: .5s;
}
.live > li:hover {
color: #333;
}
.live > li:hover::before {
-webkit-transform: scale(2);
-ms-transform: scale(2);
transform: scale(2);
opacity: 1;
text-shadow: 0 0 4px;
-webkit-transition: .1s;
transition: .1s;
}

插件版方法

插件版的方法相對來說適合新手一些,熱門的WordPress相關文章插件有以下幾個。

Related Posts for WordPress

 YARPP – Yet Another Related Posts Plugin

WPJAM-Basic(中文插件,功能強)

關於本站內容

本站整理公開產品資料與建站知識,介紹不代表實際使用測試。功能、價格、授權和支援範圍請在購買前向提供方確認。 閱讀推廣披露

SHARE YOUR QUESTIONS

交流與討論

0 條已稽核討論

分享你的問題和經驗。請勿釋出廣告、敏感資訊或未經核實的評價。

KEEP EXPLORING

你可能還想了解

繼續發現適合你的主題、工具與建站知識。

檢視全部
Fiverr 建站服務

LET’S TALK ABOUT YOUR WEBSITE

把你的建站想法,告訴我們。

提交即表示你瞭解 隱私政策。請勿傳送密碼或支付資訊。

YOUR SHORTLIST

我的諮詢清單

把感興趣的主題與服務放在一起,統一提交諮詢。

A LITTLE HELP FOR YOUR NEXT STEP

為你的好想法,
找到合適的幫手。

正在考慮建站服務、主機或工具?這些推薦可以作為了解方案的起點。

以上為推廣連結,購買前請核實服務範圍與費用。