同样的,此文也是对网友做过的东西修改了下,这个修改版的优点在于,如果相关日志找不到,那么就输出随机日志。
原版php代码从老万那粘过来(地址:WordPress 非插件相关日志的两种方法 中的方法二)
<h3>相关日志</h3>
<ul>
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>10,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title();?>
<?php comments_number(' ','(1)','(%)'); ?>
</a>
</li>
<?php endwhile;
}
}
wp_reset_query();
?>
</ul>
我的修改版php代码(放在single.php中合适位置)
<ul id="randomposts">
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>10,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h3>相关日志</h3>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_time('Y年m月d日') ?>
-- <a href="<?php the_permalink() ?>" rel="bookmark" title="查看>>
<?php the_title_attribute(); ?>">
<?php the_title(); ?>
<?php comments_number(' ','(1)','(%)'); ?>
</a> </li>
<?php
endwhile;
}else{
echo '<h3>随机日志</h3>';
$posts = get_posts('numberposts=10&orderby=rand');
foreach($posts as $post) {
setup_postdata($post);
echo '<li>';
the_time('Y年m月d日');
echo ' -- <a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
$post = $posts[0];
}
wp_reset_query();
}
?>
</ul>
CSS代码
#randomposts h3{color:#F60;margin-left:5px;padding:5px 0}
#randomposts li{list-style:circle;margin:0 0 7px 40px;font-size:12px}
我现在用过的是插件!!还是代码比较好
我复制粘贴后,出现:Parse error: syntax error, unexpected ‘>’ in /home/yrftx/domains/yrftx.com/public_html/wp-content/themes/inove/single.php on line 60 这是什么原因?
看看哪里是不是粘错了?
我也不用插件了。
呵呵 插件还是用的少好些
这个先收藏一下,以后有时间折腾,这样我的wordpress基本没插件了
當 tags 文章數量不足, 取 category 補足. http://kan.willin.org/?p=1318
:(13): 大师果然不同凡响
果然是高人啊 哈哈
:(16): 文盲路过一下
这个不错 不懂插件的人很适合
呵呵,只要知道怎么放代码就行了 :(1):
能不用插件尽量不用是对的。
嗯,插件多了有不少问题
有空换你这个试试。
:(11): 这个可以用的
学习一下!
:(4): 好好学习 天天向上
不错。我的css是调用wp relate那个插件的。收藏下,试试你的css看。
我这个CSS比较简陋 呵呵~~
这个方法不错,收藏了。
:(1): 对大家有用就好
这个好,减少插件带来的负担
插件要考虑的比较全面,同时也就变的臃肿了
哈哈,看不懂,呵呵。
呵呵,只要知道怎么用就行了
我的现在好像就是这样的
主题自带的~
:(2): inove好像没带啊
挺实用
嗯,都是些常用的东西
这个没理由不支持一下哈,话说你没用代码高亮插件吗?
是啊,一直也没弄这个,有空了找个玩玩
呵呵,都是我想要改的好东西啊~
:(24): 那就拿去用吧
偶也是用的代码 哇咔咔~ :(7):
代码好啊,可以随意鼓捣