EEDU Blog: 博客 ·  资讯 ·  论坛 ·  留言
登录 新用户? 注册   |  

ahaoxie's blog

订阅博客:

环境生态网站长

阅读全文

20条实用的WordPress PHP代码

假如你是一个比较喜欢折腾Wordpress主题的人,一定领教过PHP代码的强大作用和难度,可能你会因为要快速让自己的主题实现某些功能而在网上苦苦搜寻相关的代码,当你看到这篇文章的时候,你不用再如此奔波了,我已经收集了20个现成的Wordpress PHP代码,这些代码能够为你实现诸如“相关文章”,“最近更新的文章或页面”,“最近评论”,“评论最多文章”,等等功能,你不妨往下看,一定有你需要的。

显示最近文章

显示最近5篇文章:

<?php query_posts('showposts=5'); ?><ul><?php while (have_posts()) : the_post(); ?><li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li><?php endwhile;?></ul>
显示最近更新的文章或页面
<?php$today = current_time('mysql', 1);$howMany = 5; //你要显示的文章数目
if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):?><h2><?php _e("Recent Updates"); ?></h2><ul><?phpforeach ($recentposts as $post) {if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);echo "<li><a href='".get_permalink($post->ID)."'>";the_title();echo '</a></li>';}?></ul><?php endif; ?>
显示最近评论
<?phpglobal $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_excerptFROM $wpdb->commentsLEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =$wpdb->posts.ID)WHERE comment_approved = '1' AND comment_type = '' ANDpost_password = ''ORDER BY comment_date_gmt DESCLIMIT 10";$comments = $wpdb->get_results($sql);$output = $pre_HTML;$output .= "n<ul>";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 .= "n</ul>";$output .= $post_HTML;echo $output;?>
显示评论最多文章
<?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 $topten) {$postid = $topten->ID;$title = $topten->post_title;$commentcount = $topten->comment_count;if ($commentcount != 0) { ?><li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li><?php } } ?>
显示分类目录
<h2>分类目录</h2><ul><?php wp_list_cats('sort_column=name'); ?></ul>
以下拉菜单的形式显示分类目录
<form action="<?php bloginfo('url'); ?>/" method="get"><?php$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); echo $select; ?><noscript><input type="submit" value="View" /></noscript></form>
显示文章归档
<h2>文章归档</h2><ul><?php wp_get_archives('type=monthly'); ?></ul>
以下拉菜单的形式显示文章归档
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'><option value=""><?php echo attribute_escape(__('Select Month')); ?></option><?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>
显示页面导航
<h2>页面</h2><ul><?php wp_list_pages('title_li='); ?></ul>
显示全球通用头像(WordPress 2.5+)
<?php if(function_exists(’get_avatar’)){ echo get_avatar($comment, ‘50?);} ?>
显示友情链接
<ul><?php get_links_list(); ?></ul>
显示“管理”
<ul><?php wp_register(); ?><li><?php wp_loginout(); ?></li><li><a href="http://www.wordpress.org/">WordPress</a></li><?php wp_meta(); ?><li><a href="http://validator.w3.org/check?uri=referer">XHTML</a></li></ul>
显示子页面
<?php$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');if ($children) { ?><ul> <?php echo $children; ?></ul>
<?php } ?>
显示WordPress标签
<?php the_tags(); ?>
显示WordPress标签云
<?php wp_tag_cloud('smallest=8&largest=36&'); ?>
新建一个页面模板
<?php
/* Template Name: Portfolio */
?>
动态标题标签
<title><?phpif (is_home()) { echo bloginfo('name');} elseif (is_404()) {echo '404 Not Found';} elseif (is_category()) {echo 'Category:'; wp_title('');} elseif (is_search()) {echo 'Search Results';} elseif ( is_day() || is_month() || is_year() ) {echo 'Archives:'; wp_title('');} else {echo wp_title('');}?></title>
单独页面显示PHP

Allows you to display plugins and such on a single page (replace home with the page you want it to only appear on):

<?php if ( is_home() ) { include ('file.php'); } ?>
显示网站外部的RSS Feed
<?php include_once(ABSPATH.WPINC.'/rss.php');wp_rss('http://wpforums.com/external.php?type=RSS2', 5); ?>
显示最近的Twitter 信息
<?php// Your twitter username.$username = "你的Twitter用户名";// Prefix - some text you want displayed before your latest tweet.// (HTML is OK, but be sure to escape quotes with backslashes: for example href="link.html")$prefix = "";// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)$suffix = "";$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";function parse_feed($feed) {$stepOne = explode("<content type="html">", $feed);$stepTwo = explode("</content>", $stepOne[1]);$tweet = $stepTwo[0];$tweet = str_replace(”&lt;”, “<”, $tweet);$tweet = str_replace(”&gt;”, “>”, $tweet);return $tweet;}$twitterFeed = file_get_contents($feed);echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);?>

出处:http://www.chenchunlin.info/20-wordpress-php-codes.html

发表于: 2012-02-12 03:07 | 全文(查看: 151) | 评论(0) | 本文地址 | 收藏 
分类: 小窍门(95)   标签: wordpress  新知  

"20条实用的WordPress PHP代码" 的评论: (共 0 条)

你对本文的评论:

登录后再作评论
Page was generated in 1907 milliseconds