内容页相关文章代码

今天给主题增加了内容页的相关文章。开始想用仿无觅插件的代码,不过没操作成功。不知是代码本身的问题还是wordpress复制后的不人性的问题,我没细看主要是因为还是倾向于纯文字的。这部分代码是weisaysimple主题里的,放到我现在用的主题里也合适的很。效果如下:

neirongxiangguan
以下代码加大single.php相应位置
div class="relatedposts">
h3>您可能还会对这些文章感兴趣!</h3>
<ul>
<?php
$post_num = 12;
global $post;
$exists_related_ids = array();
$tmp_post = $post;
$tags = ''; $i = 0;
$exists_related_ids[] = $post->ID;
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(); ?>" rel="bookmark" title="详细阅读 <?php the_title(); ?>"><?php the_title(); ?><?php comments_number(' ','(1)','(%)'); ?></a></li>
<?php
$exists_related_ids[] = $post->ID;
$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.'&orderby=rand&category='.$cats.'&exclude='. implode(",", $exists_related_ids));
foreach($myposts as $post) {
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>" rel="bookmark" title="详细阅读 <?php the_title(); ?>"><?php the_title(); ?><?php comments_number(' ','(1)','(%)'); ?></a></li>
<?php
}
}
$post = $tmp_post; setup_postdata($post);
?></ul>
</div>
以下代码加到css中:
.relatedposts{}
.relatedposts ul{margin-top:5px;}
.relatedposts li{
float:left;display:block;
width:300px;height:22px;
overflow:hidden;
line-height:22px;
padding-right:8px;
}
如果想与tag分开,可将relatedposts{}的花括号中加如下代码:
font-size:12px;clear:both;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;color:#999;border-top:1px solid #EEE;margin:20px -30px 0 -30px;padding:10px 30px 0 30px

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: