WordPress模闆基本檔案

style.css 樣式表檔案
index.php 首頁檔案
single.php 日志單頁檔案
page.php 頁面檔案
archvie.php 分類和日期存檔頁檔案
searchform.php 搜尋表單檔案
search.php 搜尋頁面檔案
comments.php 留言區域檔案(包括留言清單和留言框)
404.php 404錯誤頁面
header.php 網頁頭部檔案
sidebar.php 網頁側邊欄檔案
footer.php 網頁底部檔案
View Code
WordPress Header頭部 PHP代碼
注: 也就是位于<head>和</head>之間的PHP代碼

<?php bloginfo(’name’); ?> 網站标題
<?php wp_title(); ?> 日志或頁面标題
<?php bloginfo(’stylesheet_url’); ?> WordPress主題樣式表檔案style.css的相對位址
<?php bloginfo(’pingback_url’); ?> WordPress部落格的Pingback位址
<?php bloginfo(’template_url’); ?> WordPress主題檔案的相對位址
<?php bloginfo(’version’); ?> 部落格的WordPress版本
<?php bloginfo(’atom_url’); ?> WordPress部落格的Atom位址
<?php bloginfo(’rss2_url’); ?> WordPress部落格的RSS2位址
<?php bloginfo(’url’); ?> WordPress部落格的絕對位址
<?php bloginfo(’name’); ?> WordPress部落格的名稱
<?php bloginfo(’html_type’); ?> 網站的HTML版本
<?php bloginfo(’charset’); ?> 網站的字元編碼格式
WordPress 主體模闆 PHP代碼

<?php the_content(); ?> 日志内容
<?php if(have_posts()) : ?> 确認是否有日志
<?php while(have_posts()) : the_post(); ?> 如果有,則顯示全部日志
<?php endwhile; ?> 結束PHP函數”while”
<?php endif; ?> 結束PHP函數”if”
<?php get_header(); ?> header.php檔案的内容
<?php get_sidebar(); ?> sidebar.php檔案的内容
<?php get_footer(); ?> footer.php檔案的内容
<?php the_time(’m-d-y’) ?> 顯示格式為”02-19-08″的日期
<?php comments_popup_link(); ?> 顯示一篇日志的留言連結
<?php the_title(); ?> 顯示一篇日志或頁面的标題
<?php the_permalink() ?> 顯示一篇日志或頁面的永久連結/URL位址
<?php the_category(’, ‘) ?> 顯示一篇日志或頁面的所屬分類
<?php the_author(); ?> 顯示一篇日志或頁面的作者
<?php the_ID(); ?> 顯示一篇日志或頁面的ID
<?php edit_post_link(); ?> 顯示一篇日志或頁面的編輯連結
<?php get_links_list(); ?> 顯示Blogroll中的連結
<?php comments_template(); ?> comments.php檔案的内容
<?php wp_list_pages(); ?> 顯示一份部落格的頁面清單
<?php wp_list_cats(); ?> 顯示一份部落格的分類清單
<?php next_post_link(’ %link ‘) ?> 下一篇日志的URL位址
<?php previous_post_link(’%link’) ?> 上一篇日志的URL位址
<?php get_calendar(); ?> 調用月曆
<?php wp_get_archives() ?> 顯示一份部落格的日期存檔清單
<?php posts_nav_link(); ?> 顯示較新日志連結(上一頁)和較舊日志連結(下一頁)
<?php bloginfo(’description’); ?> 顯示部落格的描述資訊
其它的一些WordPress模闆代碼

/%postname%/ 顯示部落格的自定義永久連結
<?php the_search_query(); ?> 搜尋表單的值
<?php _e(’Message’); ?> 列印輸出資訊
<?php wp_register(); ?> 顯示注冊連結
<?php wp_loginout(); ?> 顯示登入/登對外連結接
<!–next page–> 在日志或頁面中插入分頁
<!–more–> 截斷日志
<?php wp_meta(); ?> 顯示管理者的相關控制資訊
<?php timer_stop(1); ?> 顯示載入頁面的時間
<?php echo get_num_queries(); ?> 顯示載入頁面查詢
1. wordpress調用最新文章
WordPress最新文章的調用可以使用一行很簡單的模闆标簽wp_get_archvies來實作. 代碼如下:
<?php get_archives(‘postbypost’, 10); ?> (顯示10篇最新更新文章)
或者
<?php wp_get_archives(‘type=postbypost&limit=20&format=custom’); ?>
後面這個代碼顯示你部落格中最新的20篇文章,其中format=custom這裡主要用來自定義這份文章清單的顯示樣式。具體的參數和使用方法你可 以參考官方的使用說明- wp_get_archvies。(fromat=custom也可以不要,預設以UL清單顯示文章标題。)
補充: 通過WP的query_posts()函數也能調用最新文章清單, 雖然代碼會比較多一點,但可以更好的控制Loop的顯示,比如你可以設定是否顯示摘要。具體的使用方法也可以檢視官方的說明。
2. wordpress調用随機文章

<?php
$rand_posts = get_posts(‘numberposts=10&orderby=rand’);
foreach( $rand_posts as $post ) :
?>
<!–下面是你想自定義的Loop–>
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>
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
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
‘tag__in’ => array($first_tag),
‘post__not_in’ => array($post->ID),
‘showposts’=>10,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title();?> <?php comments_number(‘ ‘,’(1)’,'(%)’); ?></a></li>
<?php
endwhile;
}
}
wp_reset_query();
?>
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
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,14) 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;?>
7.wordpress調用含gravatar頭像的評論輸出

<?php
global $wpdb;
$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″;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$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>”;
}
$output .= $post_HTML;
$output = convert_smilies($output);
echo $output;
?>
上面代碼把comment_author的值改成你的ID,18是頭像大小,10是評論數量。
8.wordpress調用網站統計大全

1、日志總數:
<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>
2、草稿數目:
<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?>
3、評論總數:
<?php echo $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->comments”);?>
4、成立時間:
<?php echo floor((time()-strtotime(“2008-8-18″))/86400); ?>
5、标簽總數:
<?php echo $count_tags = wp_count_terms(‘post_tag’); ?>
6、頁面總數:
<?php $count_pages = wp_count_posts(‘page’); echo $page_posts = $count_pages->publish; ?>
7、分類總數:
<?php echo $count_categories = wp_count_terms(‘category’); ?>
8、連結總數:
<?php $link = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = ‘Y’”); echo $link; ?>
9、使用者總數:
<?php $users = $wpdb->get_var(“SELECT COUNT(ID) FROM $wpdb->users”); echo $users; ?>
10、最後更新:
<?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判斷語句

is_single()
判斷是否是具體文章的頁面
is_single(’2′)
判斷是否是具體文章(id=2)的頁面
is_single(’Beef Stew’)
判斷是否是具體文章(标題判斷)的頁面
is_single(’beef-stew’)
判斷是否是具體文章(slug判斷)的頁面
comments_open()
是否留言開啟
pings_open()
是否開啟ping
is_page()
是否是頁面
is_page(’42′)
id判斷,即是否是id為42的頁面
is_page(’About Me’)
判斷标題
is_page(’about-me’)
slug判斷
is_category()
是否是分類
is_category(’6′)
id判斷,即是否是id為6的分類
is_category(’Cheeses’)
分類title判斷
is_category(’cheeses’)
分類 slug判斷
in_category(’5′)
判斷目前的文章是否屬于分類5
is_author()
将所有的作者的頁面顯示出來
is_author(’1337′)
顯示author number為1337的頁面
is_author(’Elite Hacker’)
通過昵稱來顯示目前作者的頁面
is_author(’elite-hacker’)
下面是通過不同的判斷實作以年、月、日、時間等方式來顯示歸檔
is_date()
is_year()
is_month()
is_day()
is_time()
判斷目前是否是歸檔頁面
is_archive()
判斷是否是搜尋
is_search()
判斷頁面是否404
is_404()
判斷是否翻頁,比如你目前的blog是http://domain.com 顯示http://domain.com?paged=2的時候,這個判斷将返 回真,通過這個函數可以配合is_home來控制某些隻能在首頁顯示的界面,
例如:
<?php if(is_single()):?>
//這裡寫你想顯示的内容,包括函數
<?php endif;?>
或者:
<?php if(is_home() && !is_paged() ):?>
//這裡寫你想顯示的内容,包括函數
<?php endif;?>
10.wordpress 非插件調用評論表情

<!–smilies–>
<?php
function wp_smilies() {
global $wpsmiliestrans;
if ( !get_option(‘use_smilies’) or (empty($wpsmiliestrans))) return;
$smilies = array_unique($wpsmiliestrans);
$link=”;
foreach ($smilies as $key => $smile) {
$file = get_bloginfo(‘wpurl’).’/wp-includes/images/smilies/’.$smile;
$value = ” “.$key.” “;
$img = “<img src=\”{$file}\” alt=\”{$smile}\” />”;
$imglink = htmlspecialchars($img);
$link .= “<a href=\”#commentform\” title=\”{$smile}\” onclick=\”document.getElementById(‘comment’).value += ‘{$value}’\”>{$img}</a> ”;
}
echo ‘<div>’.$link.’</div>’;
}
?>
<?php wp_smilies();?>
<!–smilies—>
将以上代碼複制到 comments.php 中合适的位置。
原文:黃聰
連結:http://www.cnblogs.com/huangcong/archive/2011/07/04/2097764.html