首页 > 解决方案 > 我如何在每个帖子的第二段之后立即显示一个相关的帖子

问题描述

此处显示的代码 在我的 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();

?>

标签: phphtmlwordpress

解决方案


推荐阅读