willin相关文章代码修正

一个月前,应该是吧,记不清了,a.shun 就跟我说过 willin 的相关代码有问题,老是出现当前文章本身,因为忙我就一直没去分析代码。

直到昨晚,我在回复评论时看到自己博客的相关文章才突然想起,于是分析了代码一下,a.shun 所说的问题很容易解决,就是去掉下面 willin 相关文章代码里面第 5 行的注释即可——即去掉前面的 2 条斜杠。

如下面 willin 相关文章原代码(提取自 A9 主题),把下面的 //$exclude_id = $post->ID; // 單獨使用要開此行 改为 $exclude_id = $post->ID; // 單獨使用要開此行

<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5; // 數量設定.
//$exclude_id = $post->ID; // 單獨使用要開此行
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) { $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->name . ',';
$args = array(
	'post_status' => 'publish',
	'tag_slug__in' => explode(',', $tags), // 只選 tags 的文章.
	'post__not_in' => explode(',', $exclude_id), // 排除已出現過的文章.
	'caller_get_posts' => 1, // 排除置頂文章.
	'orderby' => 'comment_date', // 依評論日期排序.
	'posts_per_page' => $post_num
);
query_posts($args); // query_posts() since 2.0.0 /wp-includes/classes.php
 while( have_posts() ) { the_post(); ?>
    <li>&#9829; <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
    $exclude_id .= ',' . $post->ID; $i ++;
 } wp_reset_query();
}
if ( $i < $post_num ) { // 當 tags 文章數量不足, 再取 category 補足.
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
	'category__in' => explode(',', $cats), // 只選 category 的文章.
	'post__not_in' => explode(',', $exclude_id),
	'caller_get_posts' => 1,
	'orderby' => 'comment_date',
	'posts_per_page' => $post_num - $i
);
query_posts($args);
 while( have_posts() ) { the_post(); ?>
    <li>&#9829; <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
    $i ++;
 } wp_reset_query();
}
if ( $i  == 0 )  echo '<li>尚無相關文章</li>';
?>
</ul>

本来这样就解决了,但随即 a.shun 又发现貌似我博客的都是取得同分类的文章,而不是先取得同标签的文章。

于是我又重新分析代码,发现我的博客文章中文标签名的取不到相关文章,但英文标签的文章就能取到相关文章,看来代码问题出在标签(tag)的获取上。

经过细心发现,willin 忘了考虑中文博客很多标签名和标签的别名(slug)是不同的,而代码取得的是当前文章的标签名,而获取相关文章的代码用的是别名(slug),所以才会出现有些标签的文章不能获取相关文章。(这段很拗口。。。看得懂的看看,看不懂就飘吧。)

下面是我纠正后的代码,具体效果看我博客文章后面的相关文章。
2010.11.30 Update: 漏了个$i++和忘了去掉文章标题截取,请Copy过[2010.11.30前]的朋友重新Copy)

<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5; // 數量設定.
$exclude_id = $post->ID; // 單獨使用要開此行 //zww: edit
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
	$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; //zww: edit
	$args = array(
		'post_status' => 'publish',
		'tag__in' => explode(',', $tags), // 只選 tags 的文章. //zww: edit
		'post__not_in' => explode(',', $exclude_id), // 排除已出現過的文章.
		'caller_get_posts' => 1,
		'orderby' => 'comment_date', // 依評論日期排序.
		'posts_per_page' => $post_num
	);
	query_posts($args);
	while( have_posts() ) { the_post(); ?>
		<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
	<?php
		$exclude_id .= ',' . $post->ID; $i ++;
	} wp_reset_query();
}
if ( $i < $post_num ) { // 當 tags 文章數量不足, 再取 category 補足.
	$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
	$args = array(
		'category__in' => explode(',', $cats), // 只選 category 的文章.
		'post__not_in' => explode(',', $exclude_id),
		'caller_get_posts' => 1,
		'orderby' => 'comment_date',
		'posts_per_page' => $post_num - $i
	);
	query_posts($args);
	while( have_posts() ) { the_post(); ?>
		<li><a rel="bookmark" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
	<?php $i++;
	} wp_reset_query();
}
if ( $i  == 0 )  echo '<li>没有相关文章!</li>';
?>
</ul>

有用这段代码的朋友自己纠正吧,willin 那里我也通知了,不过他退隐“博湖”好一段时间了,期待他的复出。

zww
or
oooo

“willin相关文章代码修正”有157条评论

  1. [...] zww的《willin相关文章代码修正》中的通过文章标签获取相关文章,其中的the_title()函数放在我这边不知为何一直获取不了,改为get_the_title()才可以。他的理念是,先通过标签取相关文章,如果不够用分类中的文章补齐。 [...]

  2. 我新手 says:

    现在就差随机文章的代码了,求助 博主在吗 在线等

    1. zwwooooo says:

      @我新手
      随机文章我写过,自己搜一下

  3. 我新手 says:

    我跟那位仁兄一样 侧边栏用了留言墙,再用随机文章的无法显示评论 :twisted:

    1. zwwooooo says:

      @我新手
      如果你用的是“代码武装”那2篇文章的话,在有些主题上会出现冲突,你用下面的试试吧

      <?php
      	$tmp_post = $post;
      	$rand_posts = get_posts( array(
      		'post_type'   => $post_type,
      		'numberposts' => $num,
      		'orderby'     => 'rand'
      	) );
      	foreach( $rand_posts as $post ) : setup_postdata($post);
      		$output .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
      	endforeach; $post = $tmp_post; setup_postdata($post);
      	echo $output;
      ?>
    2. 我新手 says:

      大哥我把这个复制进去了,,,但是左边跟右边不对称,,帮我看看什么毛病 我新手

    3. zwwooooo says:

      @我新手
      样式问题自己调整吧。

  4. […] zww的《willin相关文章代码修正》中的通过文章标签获取相关文章,其中的the_title()函数放在我这边不知为何一直获取不了,改为get_the_title()才可以。他的理念是,先通过标签取相关文章,如果不够用分类中的文章补齐。 […]

  5. 雨林亭 says:

    已经用上.感谢分享!

  6. helsing says:

    Z大叔请教一个问题,我想获取文章的所有图片地址,然后在N个DIV中罗列出来,不是LI。希望帮我修改一下代码吧,我实在弄不懂...谢谢!
    global $post, $posts;
    $imageslists = $matches [0] [0];
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $PictureAmount=count($matches[0]);

    for($i=0;$i<$PictureAmount;$i++){
    echo '<div class="slide" data-mightyslider="cover: \'' .$imageslists. '\'"></div>';
    }
    真不知道怎么遍历出所有图片,并全部输出... :?:

    1. zwwooooo says:

      @helsing
      试试把

      for($i=0;$i<$PictureAmount;$i++){
      echo '<div class="slide" data-mightyslider="cover: \'' .$imageslists. '\'"></div>';
      }

      改为

      if ( $PictureAmount > 0 ) {
      	foreach ($matches[0] as $img) { ?>
      		<img src="<?php echo $img; ?>" alt="">
      	<?php }
      }

      PS:

      <img src="<?php echo $img; ?>" alt="">

      要如何输出就看你的需求了。

    2. helsing says:

      @zwwooooo
      太谢谢了,图片倒是能找到地址,但是输出的时候位置出现诡异的错位...
      global $post, $posts;
      $imageslists = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $PictureAmount = count($matches[0]);
      if ( $PictureAmount > 0 ) {
      foreach ($matches[0] as $img) {
      echo '<div class="slide" data-mightyslider="cover: \'';
      echo $img. '\'"></div>';
      }
      }

      我想把地址放到 data-mightyslider 属性里面,输出测试页面显示的却是
      <div class="slide" alt="" src="http://readate.com/wp-content/uploads/2015/04/logo_creation_1_xml.jpg" data-mightyslider="cover: '<img class=">'"></div> 够奇怪吧...不知道哪里出现的问题。 :cry:

    3. helsing says:

      @zwwooooo
      怎么好像 $img 处理后是一个标签,而不是url 链接呢!

    4. helsing says:

      @zwwooooo
      大叔,我已经搞点了,谢谢。试出了问题在哪里,谢了 :smile:

    5. zwwooooo says:

      @helsing
      哦,$img 是 整个 img 标签,哈,我忘了。我是凭记忆给你的代码,具体要自己去测试了。

    6. helsing says:

      @zwwooooo
      还有个问题,循环输出的 li 标签 class 属性怎么才能自定义循环添加 class 属性呢?比如我自定义,$classname[] ={small big, large} 想按照这个顺序给 添加不同的 Class 属性,如何实现?谢谢!C#的数组应该不能用在 WP 中吧! :cry:

    7. helsing says:

      @zwwooooo
      还有个问题,循环输出的 li 标签 class 属性怎么才能自定义循环添加 class 属性呢?比如我自定义,$classname[] ={small big, large} 想按照这个顺序给 <li class = "$classname"> 添加不同的 Class 属性,如何实现?谢谢!C#的数组应该不能用在 WP 中吧!被吃了...

    8. zwwooooo says:

      @helsing
      ...你写数组之类的要去看php基础...哪能乱套呢?
      php数组是这样的:

      $classname = array('small', 'big', 'large');

      然后你在foreach前面来个计数器 $i=0; 循环后面自加计数 $i++;。

      class="<?php echo $classname[$i]; ?>"

      但这样写法要注意$classname数组的个数对不上会报错吧。而且这些只要有点php基础或者查看下就能写/修改,还是给例子,剩下的自己去折腾吧:

      <?php
      if ( $PictureAmount > 0 ) {
      	$classes = array('small', 'big', 'large');
      	$max = count($classes);
      	$i = 0;
      	foreach ($matches[0] as $img) { ?>
      		<img class="<?php echo ($i<$max) ? $classes[$i] : ''; ?>" src="<?php echo $img; ?>" alt="">
      	<?php ++$i; }
      }
      ?>
    9. helsing says:

      @zwwooooo
      我现在只想加在ul里面的li上面,和图片已经没关系了,目的是:获取3篇文章用ul li 输出,li里面输出这篇文章的几张图片即可!看看我的代码好像有问题!复杂的代码确实不会改...学习中...抽空帮我修正一下下面代码,就大功告成了! :razz:

      <ul class="items clearfix">
      <?php while(have_posts()) : the_post(); ?>

      <?php
      $classes = array('small', 'big', 'large');
      $max = count($classes);
      $i = 0;
      ?>
      <li class="<?php echo ($i<$max) ? $classes[$i] : ''; ?>" >

    10. helsing says:

      @zwwooooo
      嗯,凭借经验搞定 加了个 ++$i; 即可!!再次谢谢大叔 :oops:

    11. helsing says:

      @zwwooooo
      还是不行,class 全是第一个类名 small ,没有递增... :cry:

    12. helsing says:

      @zwwooooo
      真的搞定了,谢谢 :shock: 评论了好多啊,周末快乐 :cool:

    13. helsing says:

      @zwwooooo
      这真的是最后一个问题了!!!在首页我用下面的代码通过ID获取文章,需要怎么修改才能不影响主循环的文章呢?我调用后多少篇,首页就少多少篇文章...

      <?php $posts = get_posts("numberposts=5&post_type=any&include=14233,14236,1372,1425,1417"); ?>

      这代码影响了首页的显示... :neutral:

    14. helsinglee says:

      @zwwooooo 老大在吗?我以前的数据都丢失了,问一下,上面那个是怎么遍历输出url的哦?因为那个$img确实是个标签,而不是地址!
      global $post, $posts;
      $imageslists = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $PictureAmount = count($matches[0]);
      if ( $PictureAmount > 0 ) {
      foreach ($matches[0] as $img) {
      echo '<div class="slide" data-mightyslider="cover: \'';
      echo $img. '\'"></div>';
      }
      }
      谢谢大叔帮瞧瞧,自己给忘了怎么搞出来的 :arrow:

    15. helsinglee says:

      @zwwooooo 再帮我看看<?php $posts = get_posts("numberposts=4&post_type=any&include=1,5,74"); if($posts) : foreach( $posts as $post ) : setup_postdata( $post ); ?>
      <?php while(have_posts()) : the_post(); ?>
      <?php $classes = array('small', 'big', 'large');
      $max = count($classes); $i = 0; ++$i; ?>
      <li class="<?php echo ($i<$max) ? $classes[$i] : ''; ?>" >
      这个给3个ID的文章用li输出,并在li上按顺序加上class属性的方法怎么写才对哦 :cool:

    16. zwwooooo says:

      @helsinglee 试试把 foreach ($matches[1] as $img) 里面的 $matches[1] 改为 $matches[0],按道理是这样,我没测试。

    17. helsing says:

      @zwwooooo 该为[0]就是整个标签,我也忘了以前怎么做到的!大叔给你发了个邮件,麻烦看看 :razz:

    18. zwwooooo says:

      @helsing 自己调试下吧,我没办法帮你调试。用 print_r() 输出看看,然后调整参数吧

    19. helsinglee says:

      @zwwooooo <ul class="items clearfix">
      <?php while(have_posts()) : the_post(); ?>
      <?php
      $classes = array('small', 'big', 'large');
      $max = count($classes);
      $i = 0;
      ?>
      <li class="<?php echo ($i<$max) ? $classes[$i] : ''; ?>" >
      谢谢哦,这个给li添加class属性怎么才对哦,我的li全是small,没有big和large

    20. zwwooooo says:

      @helsinglee 整理

      <ul class="items clearfix">
      	<?php while(have_posts()) : the_post(); ?>
      	<?php
      	$classes = array('small', 'big', 'large');
      	$max = count($classes);
      	$i = 0;
      	?>
      	<li class="<?php echo ($i<$max) ? $classes[$i] : ''; ?>" >

      你这完全不对了吧,试试:

      <ul class="items clearfix">
      	<?php
      	$classes = array('small', 'big', 'large');
      	$max = count($classes);
      	$i = 0;
      	?>
      	<?php while(have_posts()) : the_post(); ?>
      	<li class="<?php echo ($i<$max) ? $classes[$i] : ''; ++$i; ?>" >
    21. helsinglee says:

      @zwwooooo :mrgreen: 可以了,周末快乐噢,你更新得越来越少了,,,