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)插入本篇文章里的相关文章推荐、猜你喜欢功能代码。

希望陈沩亮博客( https://www.chenweiliang.com/ ) 分享的《WordPress代码实现获取显示相关文章推荐猜你喜欢功能》,对您有帮助。

欢迎分享本文链接:https://www.chenweiliang.com/cwl-1987.html

欢迎加入陈沩亮博客的 Telegram 频道,获取最新更新!

🔔 率先在频道置顶目录获取宝贵的《ChatGPT 内容营销 AI 工具使用指南》!🌟
📚 这份指南蕴含价值巨大,🌟难逢的机遇,切勿错失良机!⏰⌛💨
喜欢就分享和按赞!
您的分享和按赞,是我们持续的动力!

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

滚动到顶部