wordpress通过当前文章的TAG调取相关文章
wordpress在网站文章页面的下面或是侧边栏,使用本文的TAG调取相关文章,使用下面几行代码即可:
<?php $post_tags = wp_get_post_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { // 获取标签列表 $tag_list[] = $tag->term_id; } // 随机获取标签列表中的一个标签 $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]; // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表 $args = array( 'tag__in' => array($post_tag), 'category__not_in' => array(NULL), // 不包括的分类ID 'post__not_in' => array($post->ID), 'showposts' => 10, // 显示相关文章数量 'caller_get_posts' => 1 ); query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); update_post_caches($posts); ?> <li><span class="fr"><?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?> 次</span><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; else : ?> <li>* 暂无相关文章</li> <?php endif; wp_reset_query(); } ?>
其中li这一行根据实际使用情况作出适当的替换即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通