WordPress YARPP 相关文章和随机文章
YARPP(yet-another-related-posts-plugin)是一个在WordPress博客上显示相关文章的plugin,YARPP的设定项目比较多,可以说是目前最完整的一个相关文章插件。但如果找不到相关文章时,只会显示“No related posts”。用以下方法就可以自动转成显示随机文章。
1、从插件主页下载文件后解压缩,上传到wp-content/plugins
2、将下列代码复制,另存档名为 yarpp-template-example.php(记得选为UTF-8格式)
Example template
Author: mitcho (Michael Yoshitaka Erlewine)
*/
?>
<?php
if ($related_query->have_posts()) >
<div class="reship">
<p><strong>相关文章:</strong>
<ul>
<?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark”><?php the_title(); ?></a><!– (<?php the_score(); ?>)–></li>
<?php endwhile; ?>
</ul>
</div>
<?php else: ?>
<div class="reship">
<p><strong>随便读读:</strong>
<ul>
<?php
$posts = get_posts('numberposts=10&orderby=rand');
foreach($posts as $post)
{
setup_postdata($post);
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
$post = $posts[0];
?>
</ul>
</div>
<?php endif; ?>
3、将yarpp-template-example.php上传至wp-content/themes/当前的主题/
4、进入WordPress后台→插件→启用Yet Another Related Posts Plugin
5、再到WordPress后台→设置→关联文章(YARPP)→在插件设定页面中,“使用自定义模板”选项打勾,模板文件选择yarpp-template-example.php
6、OK!应该已能看到相关文章或随机文章啰!
可以自行更改相关文章的样式,当然也可不加样式,若要修改样式,参考下列语法放在当前主题CSS文件即可。
.reship { margin:5px 0 0; } .reship p { padding:5px; text-align:left; color:#650; font-size:15px; } .reship ul { list-style-type:none; padding-left:5px; } .reship ul li { background:transparent url(images/ico.gif) no-repeat scroll 0 8px; line-height:24px; padding-left:14px; }
相关阅读: