WordPress相关日志函数

WordPress3836.2K阅读模式

不用插件实现一些常用功能是上上之选,之前网上盛传的不使用插件就能实现的五个常用功能一文中的相关日志函数,不是很完善,加到单篇日志后,下面的评论会变成最后一篇相关日志的评论,大概是loop出了问题。而这段代码不会出现这个弊病,并且在没有相关日志的情况下,会显示随机日志。这段代码是从一款主题中Copy出来的,原函数在没有相关日志时,显示最新文章,修改为显示随机日志。将这段代码加到single.php模板文件<?php comments_template(); ?>上面即可,可以删除相关日志插件了!

<?php
$backup = $post;
$tags = wp_get_post_tags($post->ID);
$tagIDs = array();
if ($tags) {
echo '<h4>相关日志</h4>';
echo '<ul>';
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$args=array(
'tag__in' => $tagIDs,
'post__not_in' => array($post->ID),
'showposts'=>4,<!-- 显示相关日志篇数 -->
'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(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;
echo '</ul>';
} else { ?>
<ul>
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 8));<!-- 显示随机日志篇数 -->
if (have_posts()) :
while (have_posts()) : the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;endif; ?>
</ul>
<?php }
}
$post = $backup;
wp_reset_query();
?>

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  38  访客  15  作者  5
    • cyusyu
      cyusyu 3

      去把插件换下来去

      • Yacca
        Yacca 4

        可以把这个作为一个函数来调用…

        • 小骆驼商队
          小骆驼商队 1

          博主试用了吗?我试了不好用哦,开始提示cut_str没有定义,我改成mb_substr,还是不能出来文章~

          • Chloe
            Chloe 3

            請問,我使用了但是卻連單篇文章都打不開,然後出現以下警語:
            Parse error: syntax error, unexpected ‘>’ in 主題位址/single.php on line 46
            不曉得是什麼原因呢? :?:

              • 知更鸟
                知更鸟

                @ Chloe 不同的主题函数代码有差异,比如一下些函数结束语句位置不同,可能就会造成一些错误,可以尝试将代码放到不同的位置试试

              • centicent
                centicent 1

                刚刚试了,可以用,不过,要把里面的单引号换成英文半角的。和“ ”删掉。

                  • centicent
                    centicent 1

                    @ centicent 注释的“显示相关日志篇数 显示随机日志篇数”删掉。

                      • 求索阁
                        求索阁 5

                        @ centicent 对头,把这个注释删去,就正常了···

                      • 知更鸟
                        知更鸟

                        @ centicent 嗯,WP自动把英文标点转成中文标点了,现在改了程序没问题了

                      • aisinvon
                        aisinvon 1

                        能不用插件就不用插件,这个方法拿去用啦

                        • 土野鲜
                          土野鲜 3

                          不是很懂

                          • 网络整合营销
                            网络整合营销 4

                            确实啊,不是很懂,回去试试

                            • wordpress主题书包
                              wordpress主题书包 1

                              你是代码高手 请教你个问题,麻烦你能帮忙

                              如何在文章页面调用,实现分类相关文章
                              或者如何修改成按照 按tag相关文章

                              function get_posts_related_by_taxonomy($post_id,$taxonomy,$args=array()) {
                              $query = new WP_Query();
                              $do_not_duplicate[] = $post->ID;
                              $terms = wp_get_object_terms($post_id,$taxonomy);

                              if (count($terms)) {
                              // Assumes only one term for per post in this taxonomy
                              $post_ids = get_objects_in_term($terms[0]->term_id,$taxonomy);
                              $post = get_post($post_id);

                              $args = wp_parse_args($args,array(
                              ‘post_type’ => $post->post_type, // The assumes the post types match
                              ‘post__in’ => $post_ids,
                              ‘post__not_in’ => array($post->ID),
                              ‘taxonomy’ => $taxonomy,
                              ‘term’ => $terms[0]->slug,
                              ‘posts_per_page’ => 4,
                              )

                              );

                              $query = new WP_Query($args);
                              }
                              return $query;
                              }

                              • linda
                                linda 0

                                还需要尝试.

                                • 一品香茗
                                  一品香茗 1

                                  继续开始折腾 呵呵

                                  • 星星之火
                                    星星之火 0

                                    这段代码放在侧边栏怎么没有效果

                                    • 匿名
                                      匿名 0

                                      function get_posts_related_by_taxonomy($post_id,$taxonomy,$args=array()) 这个函数直接拿走了,太棒了,即可以实现标签相关,也可以实现分类相关,支持任何自定义分类法,如能写一个函数在自定义文章类型中判断其自定义类型的分类分类法taxonomy和其自定义文章类型的自定义标签分类法taxonomy的值就太好了。 :mrgreen:

                                    匿名

                                    发表评论

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

                                    拖动滑块以完成验证