WordPress模板基本文件
注: 也就是位于<head>和</head>之间的PHP代码
WordPress 主体模板 PHP代码
其它的一些Wordpress模板代码
1. wordpress调用最新文章 WordPress最新文章的调用可以使用一行很简单的模板标签wp_get_archvies来实现. 代码如下:
后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可 以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。)
补充: 通过WP的query_posts()函数也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。
2. wordpress调用随机文章
3. wordpress调用最新留言 下面是我之前在一个Wordpress主题中代到的最新留言代码,具体也记不得是哪个主题了。该代码直接调用数据库显示一份最新留言。其中 LIMIT 10限制留言显示数量。绿色部份则是每条留言的输出样式。
- <?php
- global $wpdb;
- $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
- comment_post_ID, comment_author, comment_date_gmt, comment_approved,
- comment_type,comment_author_url,
- SUBSTRING(comment_content,1,30) AS com_excerpt
- FROM $wpdb->comments
- LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
- $wpdb->posts.ID)
- WHERE comment_approved = '1' AND comment_type = '' AND
- post_password = ''
- ORDER BY comment_date_gmt DESC
- LIMIT 10";
- $comments = $wpdb->get_results($sql);
- $output = $pre_HTML; foreach ($comments as $comment) {
- $output .= "n<li>".strip_tags($comment->comment_author)
- .":" . " <a href="" . get_permalink($comment->ID) .
- "#comment-" . $comment->comment_ID . "" title="on " .
- $comment->post_title . "">" . strip_tags($comment->com_excerpt)
- ."</a></li>";
- } $output .= $post_HTML;
- echo $output;?>
4.wordpress调用相关文章
在文章页显示相关文章
- <?php</p> <p>$tags = wp_get_post_tags($post->ID);</p> <p>if ($tags) {</p> <p>$first_tag = $tags[0]->term_id;</p> <p>$args=array(</p> <p>'tag__in' => array($first_tag),</p> <p>'post__not_in' => array($post->ID),</p> <p>'showposts'=>10,</p> <p>'caller_get_posts'=>1</p> <p>);</p> <p>$my_query = new WP_Query($args);</p> <p>if( $my_query->have_posts() ) {</p> <p>while ($my_query->have_posts()) : $my_query->the_post(); ?></p> <p><li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();?> <?php comments_number(' ','(1)','(%)'); ?></a></li></p> <p><?php</p> <p>endwhile;</p> <p>}</p> <p>}</p> <p>wp_reset_query();</p> <p>?>
5.wordpress调用指定分类的文章
- <?php $posts = get_posts( "category=4&numberposts=10" ); ?>
- <?php if( $posts ) : ?>
- <ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
- <li>
- <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
- </li>
- <?php endforeach; ?>
- </ul>
- <?php endif; ?>
6.wordpress去评论者链接的评论输出
- <?php</p> <p>global $wpdb;</p> <p>$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,</p> <p>comment_post_ID, comment_author, comment_date_gmt, comment_approved,</p> <p>comment_type,comment_author_url,</p> <p>SUBSTRING(comment_content,1,14) AS com_excerpt</p> <p>FROM $wpdb->comments</p> <p>LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =</p> <p>$wpdb->posts.ID)</p> <p>WHERE comment_approved = '1' AND comment_type = '' AND</p> <p>post_password = ''</p> <p>ORDER BY comment_date_gmt DESC</p> <p>LIMIT 10";</p> <p>$comments = $wpdb->get_results($sql);</p> <p>$output = $pre_HTML;</p> <p>foreach ($comments as $comment) {</p> <p>$output .= " <li>".strip_tags($comment->comment_author)</p> <p>.":" . " <a href="" . get_permalink($comment->ID) .</p> <p>"#comment-" . $comment->comment_ID . "" title="on " .</p> <p>$comment->post_title . "">" . strip_tags($comment->com_excerpt)</p> <p>."</a></li>";</p> <p>}</p> <p>$output .= $post_HTML;</p> <p>echo $output;?>
7.wordpress调用含gravatar头像的评论输出
- <?php</p> <p>global $wpdb;</p> <p>$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,10) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND comment_author != '郑 永' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";</p> <p>$comments = $wpdb->get_results($sql);</p> <p>$output = $pre_HTML;</p> <p>foreach ($comments as $comment) {</p> <p>$output .= " <li>".get_avatar(get_comment_author_email('comment_author_email'), 18). " <a href="" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "" title="" . $comment->post_title . " 上的评论">". strip_tags($comment->comment_author) .": ". strip_tags($comment->com_excerpt) ."</a></li>";</p> <p>}</p> <p>$output .= $post_HTML;</p> <p>$output = convert_smilies($output);</p> <p>echo $output;</p> <p>?>
上面代码把comment_author的值改成你的ID,18是头像大小,10是评论数量。
8.wordpress调用网站统计大全