WordPress企业主题开发的时候往往需要在文章页底部或侧边栏调用同分类的其它产品.
这样能够吸引访客多点击查看内容,获取更多的成交几率.
虽然之前写的 WordPress主题开发教程:纯代码显示文章底部可能感兴趣相关文章推荐列表但是很多时候某些文章的关联程度不大,这就造成很多文章底部可能并没有这种文章推荐
将下面的代码添加到你的WordPress主题的single.php (在wp-content/themes/下面的你的主题目录)
<?php $cat = get_the_category(); foreach($cat as $key=>$category){ $cat_id = $category->term_id; } $args = array('orderby' => 'rand','showposts' => 10,'cat' => $$cat_id ); $query_posts = new WP_Query(); $query_posts->query($args); while ($query_posts->have_posts()) : $query_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile;?>
注意事项: showposts后面的 10 是显示的文章数量
<?php query_posts('showposts=10&cat=1&orderby=rand'); while(have_posts()) : the_post(); ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <?php endwhile; ?>
注意事项:
showposts后面的 10 是调用的文章数量
cat后面的 1 是要调用的分类ID
如何获取分类ID?: 如何查看获取WordPress分类目录与标签和文章的ID
WordPress主题开发教程:纯代码显示文章底部可能感兴趣相关文章推荐列表
WordPress主题开发教程:文章详细页面调用文章的上一篇和下一篇文章
WordPress主题开发教程:文章详细页面调用当前文章的前5篇和后5篇文章
WordPress主题开发教程:WP-Postviews调用网站热门文章浏览点击量排名文章列表
WordPress主题开发:显示WordPress当前分类所有文章标签