php - 我如何在每个帖子的第二段之后立即显示一个相关的帖子
问题描述
此处显示的代码 在我的 function.php 中工作,但我只想显示一个相关的帖子。
这是两个不同的功能,但我想将它与下面的代码合并在一起。
这是帖子标签查询代码,它工作正常,但我想在我网站上每个帖子的第二段之后立即显示它。
<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = [];
foreach ($tags as $individual_tag) {
$tag_ids[] = $individual_tag->term_id;
}
$args = [
'tag__in' => $tag_ids,
'post__not_in' => [$post->ID],
'posts_per_page' => 1, // Number of related posts that will be shown.
'caller_get_posts' => 1,
];
$my_query = new wp_query($args);
if ($my_query->have_posts()) {
while ($my_query->have_posts()) {
$my_query->the_post(); ?>
Related: <a href="<? the_permalink() ?>"><?php the_title(); ?></a>
<? }
}
}
$post = $orig_post;
wp_reset_query();
?>
解决方案
推荐阅读
- embedded-linux - 我无法操作我的通用 I/O
- java - 从没有api的网站检索数据
- javascript - 为什么我的“返回顶部”按钮在 Firefox 上失败?
- typescript - VUE:是否有可能没有冲突的属性名称数据?
- excel - 将错误作为运行时错误“-2147467259(800004005)”:自动化错误未指定错误
- r - 在约束和二元变量中使用函数进行约束优化
- javascript - 限制在全日历中创建事件
- asp.net - 页面加载一遍又一遍
- javafx - JavaFX:PopOver 窗口的鼠标事件 (ControlsFX)
- django - django api在连接到mysql时抛出500(内部服务器)错误