WordPress代碼實現獲取顯示相關文章推薦猜你喜歡功能

忠志WordPress外掛都可以實現相關文章推薦、猜你喜歡的功能。

  • WordPress插件的優點是配置簡單,但可能對網站有一定影響。
  • 所以,很多人還是喜歡用WordPress代碼來實現相關文章、猜你喜歡的功能。
  • 但同樣地,代碼實現相關文章、猜你喜歡功能也是自相矛盾的,代碼的缺點是配置較複雜。

WordPress如何代碼實現獲取顯示相關文章猜你喜歡功能?

WordPress相關文章推薦、猜你喜歡代碼示例

以下是從標籤、分類獲取相關文章的猜你喜歡代碼:

<h3>猜你喜欢</h3>
<ul class="related_posts">
<?php
$post_num = 10; //文章数量
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
$args = array(
'post_status' => 'publish',
'tag__in' => explode(',', $tags),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i < $post_num ) {
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num - $i
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>暂无相关推荐</li>';
?>
</ul>
  • 其中 $post_num = 10; 是顯示相關文章的數量為10。
  • 如果在WordPress的頁面,是沒有標籤和分類的,則顯示“暫無相關推薦”。

WordPress怎麼做相關相似文章鏈接?

通過安裝ad-inserter插件,設置在文章內容之後(insertion:After Content)插入本篇文章裡的相關文章推薦、猜你喜歡功能代碼。

發表評論

您的郵箱地址不會被公開。 必填項已用 * 標註

回到頁首