在使用wordpress开发主题或者修改别人主题的时候,很多时候都会考虑到调用同分类下的相关的文章,或者调用同分类下的随机文章,那么,我们应该怎么做呢?
将下面代码放到主题文章页面single模板或者边栏sidebar模板适当位置即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <ul> <?php $cat = get_the_category(); foreach($cat as $key=>$category){ $catid = $category->term_id; } $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid ); $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;?> <?php wp_reset_query(); ?> </ul> |
上面就是WordPress调用同分类下的随机文章的方法,如果你想要做其他的调用,建议你去学习一下wordpress函数,以后开发主题的时候才会得心应手。
转载请保留本文链接:https://www.zhe94.com/126.html