WordPress模板基本文件 1. <?php get_archives('postbypost', 10); ?> (显示10篇最新更新文章) 复制代码 或者 1. <?php wp_get_archives(‘type=postbypost&limit=20&format=custom’); ?> 复制代码
后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可 以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。) 1. <?php 2. $rand_posts = get_posts('numberposts=10&orderby=rand'); 3. foreach( $rand_posts as $post ) : 4. ?> 5. <!--下面是你想自定义的Loop--> 6. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 7. <?php endforeach; ?> 复制代码
3. wordpress调用最新留言 1. <?php 2. global $wpdb; 3. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, 4. comment_post_ID, comment_author, comment_date_gmt, comment_approved, 5. comment_type,comment_author_url, 6. SUBSTRING(comment_content,1,30) AS com_excerpt 7. FROM $wpdb->comments 8. LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = 9. $wpdb->posts.ID) 10. WHERE comment_approved = '1' AND comment_type = '' AND 11. post_password = '' 12. ORDER BY comment_date_gmt DESC 13. LIMIT 10"; 14. $comments = $wpdb->get_results($sql); 15. $output = $pre_HTML; foreach ($comments as $comment) { 16. $output .= "n<li>".strip_tags($comment->comment_author) 17. .":" . " <a href="" . get_permalink($comment->ID) . 18. "#comment-" . $comment->comment_ID . "" title="on " . 19. $comment->post_title . "">" . strip_tags($comment->com_excerpt) 20. ."</a></li>"; 21. } $output .= $post_HTML; 22. echo $output;?> 复制代码 4.wordpress调用相关文章 1. <?php 2. 3. $tags = wp_get_post_tags($post->ID); 4. 5. if ($tags) { 6. 7. $first_tag = $tags[0]->term_id; 8. 9. $args=array( 10. 11. 'tag__in' => array($first_tag), 12. 13. 'post__not_in' => array($post->ID), 14. 15. 'showposts'=>10, 16. 17. 'caller_get_posts'=>1 18. 19. ); 20. 21. $my_query = new WP_Query($args); 22. 23. if( $my_query->have_posts() ) { 24. 25. while ($my_query->have_posts()) : $my_query->the_post(); ?> 26. 27. <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();?> <?php comments_number(' ','(1)','(%)'); ?></a></li> 28. 29. <?php 30. 31. endwhile; 32. 33. } 34. 35. } 36. 37. wp_reset_query(); 38. 39. ?> 复制代码 5.wordpress调用指定分类的文章 1. <?php $posts = get_posts( "category=4&numberposts=10" ); ?> 2. <?php if( $posts ) : ?> 3. <ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?> 4. <li> 5. <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> 6. </li> 7. <?php endforeach; ?> 8. </ul> 9. <?php endif; ?> 复制代码 6.wordpress去评论者链接的评论输出 1. <?php 2. 3. global $wpdb; 4. 5. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, 6. 7. comment_post_ID, comment_author, comment_date_gmt, comment_approved, 8. 9. comment_type,comment_author_url, 10. 11. SUBSTRING(comment_content,1,14) AS com_excerpt 12. 13. FROM $wpdb->comments 14. 15. LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = 16. 17. $wpdb->posts.ID) 18. 19. WHERE comment_approved = '1' AND comment_type = '' AND 20. 21. post_password = '' 22. 23. ORDER BY comment_date_gmt DESC 24. 25. LIMIT 10"; 26. 27. $comments = $wpdb->get_results($sql); 28. 29. $output = $pre_HTML; 30. 31. foreach ($comments as $comment) { 32. 33. $output .= "n<li>".strip_tags($comment->comment_author) 34. 35. .":" . " <a href="" . get_permalink($comment->ID) . 36. 37. "#comment-" . $comment->comment_ID . "" title="on " . 38. 39. $comment->post_title . "">" . strip_tags($comment->com_excerpt) 40. 41. ."</a></li>"; 42. 43. } 44. 45. $output .= $post_HTML; 46. 47. echo $output;?> 复制代码 7.wordpress调用含gravatar头像的评论输出 1. <?php 2. 3. global $wpdb; 4. 5. $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"; 6. 7. $comments = $wpdb->get_results($sql); 8. 9. $output = $pre_HTML; 10. 11. foreach ($comments as $comment) { 12. 13. $output .= "n<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>"; 14. 15. } 16. 17. $output .= $post_HTML; 18. 19. $output = convert_smilies($output); 20. 21. echo $output; 22. 23. ?> 复制代码
上面代码把comment_author的值改成你的ID,18是头像大小,10是评论数量。 1. 1、日志总数: 2. 3. <?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?> 4. 5. 2、草稿数目: 6. 7. <?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?> 8. 9. 3、评论总数: 10. 11. <?php echo $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");?> 12. 13. 4、成立时间: 14. 15. <?php echo floor((time()-strtotime("2008-8-18"))/86400); ?> 16. 17. 5、标签总数: 18. 19. <?php echo $count_tags = wp_count_terms('post_tag'); ?> 20. 21. 6、页面总数: 22. 23. <?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?> 24. 25. 7、分类总数: 26. 27. <?php echo $count_categories = wp_count_terms('category'); ?> 28. 29. 8、链接总数: 30. 31. <?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?> 32. 33. 9、用户总数: 34. 35. <?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?> 36. 37. 10、最后更新: 38. 39. <?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y-n-j', strtotime($last[0]->MAX_m));echo $last; ?> 复制代码
9.wordpress判断语句 1. <?php if(is_single()):?> 2. 3. //这里写你想显示的内容,包括函数 4. 5. <?php endif;?> 复制代码 10.wordpress 非插件调用评论表情 1. <!--smilies--> 2. <?php 3. 4. function wp_smilies() { 5. 6. global $wpsmiliestrans; 7. 8. if ( !get_option('use_smilies') or (empty($wpsmiliestrans))) return; 9. 10. $smilies = array_unique($wpsmiliestrans); 11. 12. $link=''; 13. 14. foreach ($smilies as $key => $smile) { 15. 16. $file = get_bloginfo('wpurl').'/wp-includes/images/smilies/'.$smile; 17. 18. $value = " ".$key." "; 19. 20. $img = "<img src="{$file}" alt="{$smile}" />"; 21. 22. $imglink = htmlspecialchars($img); 23. 24. $link .= "<a href="#commentform" title="{$smile}" onclick="document.getElementById('comment').value += '{$value}'">{$img}</a> "; 25. 26. } 27. 28. echo '<div>'.$link.'</div>'; 29. 30. } 31. 32. ?> 33. 34. <?php wp_smilies();?> 35. 36. <!--smilies—> 复制代码 |
0则评论给“wordpress常用标签调用代码大全”