定制自己的WordPress首页
在你的主题目录中拷贝、修改 page.php,注意一开始的
<?php /* Template Name: GuoshuangTemplate */ ?>
在后台的新建一个 page,并且设置该 page 的 模板 为 GuoshuangTemplate。

最后在 Settings » Reading 设置front page 为刚才这个 page 即可。

显示随机的 post
<?php
query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?><h1><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
显示外部站点的 feed
<?php include_once(ABSPATH.WPINC.’/feed.php’);
$rss = fetch_feed(‘http://feeds.feedburner.com/wpbeginner’);
$maxitems = $rss->get_item_quantity(5);
$rss_items = $rss->get_items(0, $maxitems);
?>
<ul>
<?php if ($maxitems == 0) echo ‘<li>No items.</li>’;
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href=’<?php echo $item->get_permalink(); ?>’
title=’<?php echo ‘Posted ‘.$item->get_date(‘j F Y | g:i a’); ?>’>
<?php echo $item->get_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
显示文章或者留言距现在的时间
需要这个 plugin wp relativedate,在需要输出时期的地方使用下面代码
<?php relative_post_the_date(); ?>
允许用户提交新闻
用这个插件 tdo-mini-forms,使用说明在how to allow users to submit news posts to your wordpress site
直接从标题链接到其它站点
在 functions.php 添加定义
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey==’url1′ || $pkey==’title_url’ || $pkey==’url_title’) {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo ‘<h2><a href=”‘.$link.’” rel=”bookmark” title=”‘.$title.’”>’.$title.’</a></h2>’;
}
然后把
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
改为
<?php print_post_title(); ?>
用 wordpress 做邮件订阅服务器
create-a-free-email-newsletter-service-using-wordpress
控制 post 显示条数
if ($count == “n”) { break; }
else { ?>
突出显示作者的留言
定义样式表
.authorstyle { background-color: #B3FFCC !important; }
修改 comments.php,替换下面代码
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>"></li>
为
<liauthorstyle"; echo $oddcomment; ?>"></li>
打开文章缩略图功能
how-to-add-post-thumbnails-in-wordpress
显示 feedburner 订阅数
<?php
//get cool feedburner count
$whaturl=”http://api.feedburner.com/awareness/1.0/GetFeedData?uri=your feedburner id”;//Initialize the Curl session
$ch = curl_init();//Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set the URL
curl_setopt($ch, CURLOPT_URL, $whaturl);//Execute the fetch
$data = curl_exec($ch);//Close the connection
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
echo $fb;
//end get cool feedburner count
?>
显示 twitter 数
新建 twitter.php
<?php
$tw = get_option(“twitterfollowerscount”);
if ($tw['lastcheck'] < ( mktime() %u2013 3600 ) )
{
$xml=file_get_contents(‘http://twitter.com/users/show.xml?screen_name=wpbeginner’);
if (preg_match(‘/followers_count>(.*)</’,$xml,$match)!=0) {
$tw['count'] = $match[1];
}
$tw['lastcheck'] = mktime();
update_option(“twitterfollowerscount”,$tw);
}
echo $tw['count'];
?>
在需要的地方 include
<?php include("twitter.php"); ?>
10分钟后文章才出现在 feed 中
function publish_later_on_feed($where) {
global $wpdb;if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate(‘Y-m-d H:i:s’);// value for wait; device
$wait = ’10′; // integer// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR// add SQL-sytax to default $where
$where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
}
return $where;
}add_filter(‘posts_where’, ‘publish_later_on_feed’);
随机显示头部图片
<img src="http://path_to_images/headerimage_<?php echo(rand(1,3)); ?>.gif"
width="image_width" height="image_height" alt="image_alt_text" />
设置文章过期时间
自定义域 expiration 值的格式为 mm/dd/yyyy 00:00:00
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values(‘expiration’);
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example…
the_title();
the_excerpt();
}
endwhile;
endif;
?>
删除数据库中的修订版本
这样可以减少数据库的大小。在 phpmyadmin 的 wp 数据库执行 sql
DELETE FROM wp_posts WHERE post_type = "revision";
让WP成为一个通讯录管理工具
The New & Improved Way to Turn WordPress 2.7 into a Membership Community
页面卷边效果
create-a-peel-away-effect-on-website-how-to
或者
某些文章定制自己的样式表
<?php if (is_single()) {
$customstyle = get_post_meta($post->ID, ‘customstyle’, true);
if (!empty($customstyle)) { ?>
<style type=”text/css”>
<?php echo $customstyle; ?>
<style>
<?php }
} ?>
根据类别定制 header和sidebar
<?php if (is_category(‘Blogging’)) {
get_header(‘blogging’);
} else {
get_header();
} ?>
将会采用 header-blogging.php
<?php if (is_category(‘Blogging’)) {
get_sidebar(‘blogging’);
} else {
get_sidebar();
} ?>
将会采用 sidebar-blogging.php
定制自己的登录界面
Best of Best WordPress Custom Login Page Designs
定制rss footer
关掉留言的html
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( “‘”, ‘'’, $incoming_comment['comment_content'] );return( $incoming_comment );
}// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {// Put the single quotes back in
$comment_to_display = str_replace( ‘'’, “‘”, $comment_to_display );return $comment_to_display;
特定类别的最新条目
<?php
query_posts(‘showposts=1&cat=3′);
while(have_posts()) : the_post();
?>
<ul>
<li><h3><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></h3><ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>
控制摘要长度
// Changing excerpt length
function new_excerpt_length($length) {
return 100;
}
add_filter(‘excerpt_length’, ‘new_excerpt_length’);// Changing excerpt more
function new_excerpt_more($more) {
return ‘%u2026′;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);
在个人页面显示twitter facebook信息
在 functions.php 添加
<?php
function my_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = ‘Twitter’;
//add Facebook
$contactmethods['facebook'] = ‘Facebook’;return $contactmethods;
}
add_filter(‘user_contactmethods’,'my_new_contactmethods’,10,1);
?>
在 author.php 加入
<?php echo $curauth->twitter; ?>
导航菜单
自定义显示留言
<?php
$query = “SELECT * from $wpdb->comments WHERE comment_approved= ’1′
ORDER BY comment_date DESC LIMIT 0 ,5″;
$comments = $wpdb->get_results($query);if ($comments) {
echo ‘<ul>’;
foreach ($comments as $comment) {
$url = ‘<a href=”‘. get_permalink($comment->comment_post_ID).’#comment-’.$comment->comment_ID .’” title=”‘.$comment->comment_author .’ | ‘.get_the_title($comment->comment_post_ID).’”>’;
echo ‘<li>’;
echo ‘<div>’;
echo $url;
echo get_avatar( $comment->comment_author_email, $img_w);
echo ‘</a></div>’;echo ‘<div>Par: ‘;
echo $url;
echo $comment->comment_author;
echo ‘</a></div>’;
echo ‘</li>’;
}
echo ‘</ul>’;
}
?>




