[英]WordPress load related posts using tags not working
我正在使用WordPress PHP代碼獲取當前帖子的相關帖子,但是當我使用此代碼時,它僅顯示當前帖子
我的代碼如下
$posttags = wp_get_post_tags(get_the_id(), array( 'fields' => 'slugs' ));
$args = array(
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $posttags
)
)
);
$postslist = get_posts( $args );
誰能幫我使用當前的帖子標簽獲取其他相關帖子。 謝謝
請參考此鏈接以獲取更多詳細信息: https : //developer.wordpress.org/reference/classes/wp_query/
<?php
// For use in the loop, list 3 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args = array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page' => 3,
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
if ( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
}
wp_reset_query();
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.