首页 > WordPress > 开发笔记 > 给BIT主题文章调用同标签文章列表

给BIT主题文章调用同标签文章列表

一个老不正经 2022/01/22 734围观

做SEO的小伙伴一般知道,页面的相关性,跟关键词排名息息相关,而BIT主题是为了SEO而开发,而同TAG里面的内容相关性是非常高的,所以调用tag的文章列表是必不可少的;下面是代码。

需要循环前面的代码

<?php //同标签文章列表
$post_num = 1;
global $post;
global $xbt_single_ids;
$tmp_post = $post;
$tags = ''; $i = 0;
if ( get_the_tags( $post->ID ) ) {
foreach ( get_the_tags( $post->ID ) as $tag ) $tags .= $tag->slug . ',';
$tags = strtr(rtrim($tags, ','), ' ', '-');
$myposts = get_posts('numberposts='.$post_num.'&tag='.$tags.'&exclude='.$post->ID);
foreach($myposts as $post) {
setup_postdata($post);
array_push($xbt_single_ids,$post->ID);
?>

下面是循环内容的结尾

<?php
$i += 1;
}
}
if ( $i < $post_num ) {
$post = $tmp_post; setup_postdata($post);
$cats = ''; $post_num -= $i;
foreach ( get_the_category( $post->ID ) as $cat ) $cats .= $cat->cat_ID . ',';
$cats = strtr(rtrim($cats, ','), ' ', '-');
$myposts = get_posts('numberposts='.$post_num.'&category='.$cats.'&exclude='.$post->ID);
foreach($myposts as $post) {
setup_postdata($post);
?>
<?php }
}
$post = $tmp_post; setup_postdata($post);
?>

而这两段代码中间放入需要循环调用的代码就行

展示效果是:

代码截图: