出于网站优化以及用户体验需要 wordpress 侧边栏需要显示相同tag文章。
实现方法
侧边栏支持PHP
安装插件 Samsarin PHP Widget
相同Tag调用
<ul>
<?php
$post_num = 10;
global $post;
$tmp_post = $post;
$tags = ''; $i = 0;
if ( get_the_tags( $post->ID ) ) {
foreach ( get_the_tags( $post->ID ) as $tag ) $tags .= $tag->name . ',';
$tags = strtr(rtrim($tags, ','), ' ', '-');
$myposts =get_posts('numberposts='.$post_num.'&tag='.$tags.'&exclude='.$post->ID);
foreach($myposts as $post) {
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
$post = $tmp_post; setup_postdata($post);
?>
</ul>