最近自己修改了一套WordPress主题,用到一些有用的代码,记录下:
1.调用某一分类下最新文章列表:
<div class="relran_cont"> <span><a href="http://www.jiangyu.org/category/live-is-life/">+more</a></span> <h3><?php wp_list_categories('include=146&title_li=&style=none'); ?></h3><ul> <?php query_posts('showposts=5&cat=146'); ?> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> </div>
2.调用某一分类下文章摘要及相关信息:
<?php query_posts('showposts=5&cat=16'); ?> <?php while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <div class="line"> <p class="postdate"><?php the_time("Y-m-d"); ?></p> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="阅读 <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> </div> <p> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/bullet.gif"> 栏目:<?php the_category(' , ') ?> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/bullet.gif"> <?php if(function_exists('the_views')) { the_views(); } ?> </p> <div class="entry"> <?php the_excerpt(''); ?> </div> <div class="indextag"> <div style="float:left"> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/bullet.gif"> 标签:<?php the_tags(' ' , ' , ' , ' '); ?> </div> <div style="float:right"> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/bullet.gif" alt=""/> <a href="<?php the_permalink() ?>" rel="bookmark" title="阅读全文"> 阅读全文</a> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/bullet.gif"> <?php comments_popup_link('尚无回复', '1 枚回复', '% 枚回复'); ?> </div> </div> </div> <?php endwhile; ?>
3.调用最新文章列表:
<?php query_posts('showposts=10'); ?> <ul class="random-article tab-con"> <?php while (have_posts()) : the_post(); ?> <li><b class="num-4"></b><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endwhile;?> </ul>
4.随机调用文章列表:
<ul class="random-article tab-con" style="display:none"> <?php global $post; $postid = $post->ID; $args = array( 'orderby' => 'rand', 'post__not_in' => array($post->ID), 'showposts' => 10); $query_posts = new WP_Query(); $query_posts->query($args); ?> <?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?> <li><b class="num-4"></b><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul>
5.调用回复最多文章列表:
<ul class="random-article tab-con" style="display:none"> <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10"); foreach ($result as $post) { setup_postdata($post); $postid = $post->ID; $title = $post->post_title; $commentcount = $post->comment_count; if ($commentcount != 0) { ?> <li><b class="num-4"></b><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"> <?php echo $title ?></a> <span class="views"> (<?php echo $commentcount ?>)</span></li> <?php } } ?> </ul>
6.调用浏览最多文章列表(需要安装WP-PostViews插件):
<?php if (function_exists('get_most_viewed')): ?> <ul class="hot-article"> <?php get_most_viewed('post',10); ?> </ul> <?php endif; ?>
评论