wordpress相关文章调用非插件的方法
我们在做wordpress企业网站和博客的时候,都会在文章详情页加上相关文章板块,有利于用户浏览,也便于搜索引擎收录文章。
下面给大家介绍几种常用的调用方式轻松解决wordpress相关文章。
方法一:分类相关-调用相同分类的文章做为相关文章
<ul id="cat_related"> <?php global $post; $cats = wp_get_post_categories($post->ID); if ($cats) { $args = array( 'category__in' => array( $cats[0] ), 'post__not_in' => array( $post->ID ), 'showposts' => 6, 'caller_get_posts' => 1 ); query_posts($args); if (have_posts()) { while (have_posts()) { the_post(); update_post_caches($posts); ?> <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php } } else { echo '<li>* 暂无相关文章</li>'; } wp_reset_query(); } else { echo '<li>* 暂无相关文章</li>'; } ?> </ul>
方法二:通过调用相同标签的文章
<ul id="tags_related"> <?php global $post; $post_tags = wp_get_post_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { // 获取标签列表 $tag_list[] .= $tag->term_id; } // 随机获取标签列表中的一个标签 $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]; // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表 $args = array( 'tag__in' => array($post_tag), 'category__not_in' => array(NULL), // 不包括的分类ID 'post__not_in' => array($post->ID), 'showposts' => 6, // 显示相关文章数量 'caller_get_posts' => 1 ); query_posts($args); if (have_posts()) { while (have_posts()) { the_post(); update_post_caches($posts); ?> <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php } } else { echo '<li>* 暂无相关文章</li>'; } wp_reset_query(); } else { echo '<li>* 暂无相关文章</li>'; } ?> </ul>
通过以上两种方式就可以轻松实现wordpress文章页面相关文章的功能,非常简单,希望对大家有所帮助!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END