WordPress各種標籤調用集合
wordpress是一種使用PHP語言開發的博客平臺,用戶可以在支持PHP和MySQL 數據庫的服務器上架設自己的網誌,插件衆多,易於擴充功能。安裝和使用都非常方便。目前 WordPress 已經成爲主流的 Blog 搭建平臺。這個網站就用的是WordPress構建的。
買服務器域名的可以領取下面的券
騰訊雲新客專屬福利2860元代金券
https://cloud.tencent.com/redirect.php?redirect=1025&cps_key=09ff33cfb418db124451885bff0af0c4&from=console
騰訊雲熱賣雲產品3折起
https://cloud.tencent.com/redirect.php?redirect=1014&cps_key=09ff33cfb418db124451885bff0af0c4&from=console
網站解決方案-3折特惠
https://cloud.tencent.com/redirect.php?redirect=1027&cps_key=09ff33cfb418db124451885bff0af0c4&from=console
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 網頁底部文件
WordPress Header頭部 PHP代碼
注: 也就是位於和之間的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(); ?> 顯示一份博客的分類列表
分類目錄函數wp_list_cats() 或 分類列表函數wp_list_categories()用法舉例
1、按照字母排序,並只顯示 ID 爲16、3、9和5的指定分類:
-
<?php
wp_list_categories(‘orderby=name&include=3,5,9,16‘); ?>
2、按照字母排序,顯示每個分類的日誌數,但不顯示 ID 爲10的分類
<ul>
<?php
wp_list_categories(‘orderby=name&show_count=1&exclude=10‘); ?>
</ul>
3、顯示或隱藏列表頭,在分類函數 wp_list_categories 中,title_li 這個參數用於設置或者隱藏分類列表的頭或者標題。它的默認值是:‘(__(’Categories’)’ ,這也就是爲什麼我們在不另設置分類列表標題的時候,它會顯示“Categories”的原因。如果你在這裏不設置任何參數,那麼它將什麼都不會顯示。下 面的例子是排除 ID 爲4和7並且隱藏列表頭的分類列表:
<ul>
<?php
wp_list_categories(‘exclude=4,7&title_li=‘); ?>
</ul>
接下來的例子是僅僅只顯示 ID爲5、9和23,並且列表頭顯示爲“詩歌”的分類列表:
<ul>
<?php
wp_list_categories(‘include=5,9,23&title_li=<h2>‘ . __(‘詩歌‘) . ‘</h2>‘ ); ?>
</ul>
4、僅顯示某個分類下的子分類,下面的示例代碼生成了 ID 爲8的父分類下的子分類根據其 ID 進行排序的鏈接列表(讀起來真繞口 -__-|||),它會顯示每個分類下的文章數,並且隱藏鏈接的 title 標籤中的分類描述,注意:如果父分類下沒有任何文章,那麼父分類將不會顯示
<ul>
<?php wp_list_categories(‘orderby=id&show_count=1
&use_desc_for_title=0&child_of=8‘); ?>
</ul>
這個函數裏設置的參數比較多,這裏我稍作說明:我們可以看到不同參數之間使用了“&”這個“與符號”來進行區分連接,orderby=id 按照 ID 排序,show_count=1 顯示分類下的文章數,use_desc_for_title=0 隱藏分類描述,child_of=8 指定 ID 爲8的子分類。
5、顯示帶有 RSS Feed 鏈接的分類列表,下面代碼根據分類名對分類列表排序,並顯示每個分類下的文章數和 RSS 的 Feed 鏈接。
<ul>
<?php
wp_list_categories(‘orderby=name&show_count=1&feed=RSS‘); ?>
</ul>
還可以使用 RSS 圖標代替 RSS 鏈接
<ul>
<?php
wp_list_categories(‘orderby=name&show_count=1
&feed_image=/images/rss.gif‘); ?>
</ul>
6、標記和樣式化分類列表,從上面的例子中可以看到,我們將分類列表函數: wp_list_categories() 套用在 ul 和 li 標籤裏,除此外我們還可以對其進行其它的樣式化,個人認爲這些工作直接在 CSS 裏設置即可,原文檔中的方法實際作用並不是很大,這裏我就不多做介紹,有興趣的朋友可以 參考這裏
7、style
(字符串)分類列表顯示的樣式。將分類列表的方式顯示(使用
標籤分隔列表項)。默認設置爲列表(無序列表)。有效值:
list – 默認
none
例子:
<ul>
<?php
wp_list_categories(‘orderby=name&style=none‘); ?>
</ul>
<?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(); ?> 顯示載入頁面查詢
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的顯示,比如你可以設置是否顯示摘要。具體的使用方法也可以查看官方的說明。
wordpress調用隨機文章
<?php
$rand_posts = get_posts(‘numberposts=10&orderby=rand’);
foreach( $rand_posts as $post ) :
?>
<!–下面是你想自定義的Loop–>
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調用指定分類的文章
方法1:
<?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; ?>
方法2:
<?php $posts = get_posts( "category=1&numberposts=10" ); ?>
<?php if( $posts ) : ?>
<ul class="news_ful">
<?php while (have_posts()) : the_post(); ?>
<li> <a class=" animated an4" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<label><? echo wp_trim_words( get_the_title(),24); ?></label>
<span><?php the_time('Y-m-d'); ?></span></a> </li>
<?php endwhile;wp_reset_query(); ?>
<?php endif; ?>
<?php $thiscat = get_category($cat); echo $thiscat ->name;?>調用指定分類名稱標題
<?php $cat = get_category($cid);echo $cat->slug;?> 調用指定分類別名
<?php echo get_cat_ID( $cat_name ) ?>調用指定分類名稱對應的分類ID
<?php echo get_category_link($cid) ?>調用指定分類url鏈接
//$cid爲整型變量
wp_list_categories(‘orderby=name&include=3,5,9,16‘); ?>
WordPress獲取指定分類下指定子分類
<ul class="busul">
<?php $categories = array(9,10,11,12,13);
foreach ($categories as $cid) { ?>
<?php query_posts("showposts=8&cat=$cid") ?>
<li><a class="img animated an6" href="<?php echo get_category_link($cid) ?>" title="<?php single_cat_title() ?>"><?php single_cat_title() ?></a></li>
<?php } wp_reset_query(); ?>
</ul>
或者
$args=array(
'orderby' => 'id',
'order' => 'ASC',
'child_of' => get_category_root_id($cat),
'hide_empty' => 0,
);
<?php
$categories=get_categories($args);
foreach($categories as $category) {
if(($category->term_id)==$cat){
echo '<li class="news-xians">';
}else{
echo '<li>';
}
echo '<a href="'.get_category_link( $category->term_id ).'">'.$category->name.'</a>';
echo '</shd>';
echo "\r";
}
?>
WordPress獲取指定分類下全部子分類
<?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=1&hide_empty=0&child_of=51'); ?>
代碼解釋:sort_column=name爲調用分類名;optioncount=1爲調用分類下的文章數量;hide_empty=0爲是否隱藏沒有文章的分類;child_of=51爲調用的父分類ID號,需要改爲你自己的父分類的ID號。
以上的代碼是同時調用出子分類名和子分類下的文章數,如果只想調用子分類名,不需要顯示文章數量,可以使用以下的代碼:
<?php wp_list_cats('sort_column=name&optioncount=0&hierarchical=1&hide_empty=0&child_of=51'); ?>
一、指定分類下的子分類獲取方法:
<?php wp_list_cats
('sort_column=name&optioncount=1&hierarchical=1&hide_empty=0&child_of=10'); ?>
說明:
child_of=10中的10是指某個分類的ID號。
sort_column:ID 或 name,預設爲ID,設定依 ID 值或分類名稱排序
sort_order:asc 或 desc,預設爲遞增 asc,設定排序遞增或遞減 (&sort_column=ID&sort_order=desc )
二、指定頁面下的子頁面獲取方法:
<?php wp_list_pages(‘hide_empty=0&child_of=5&title_li=’); ?>
說明:
child_of=5中的5是指某個頁面的ID號。
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 .= “ <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 .= “ <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非插件同步twitter
<?php
require_once (ABSPATH . WPINC . ‘/class-feed.php’);
$feed = new SimplePie();
$feed->set_feed_url(‘http://feeds.feedburner.com/agting′);
$feed->set_file_class(‘WP_SimplePie_File’);
$feed->set_cache_duration(600);
$feed->init();
$feed->handle_content_type();
$items = $feed->get_items(0,1);
foreach($items as $item) {
echo ‘<a target=”_blank” rel=”external nofollow” title=”Follow Me on Twitter” href=”http://twitter.com/agting″>@用戶名</a>: ‘.$item->get_description();
}
?>
代碼中的agting改成你的twitter用戶名,改成你的名字。
另一種調用方法需要你的空間是國外主機:
<?php
// Your twitter username.
$username = “wange1228″;
// 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”)
// 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(“<”, “<”, $tweet);
$tweet = str_replace(“>”, “>”, $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
總結一下這個方法的特點:
1、非插件.
2、不用驗證用戶名和密碼,也就是說你可以指定調用任何一個人的 tweet.
3、可以自定義 tweet 信息後顯示的文字,就是 $suffix = “”; 這裏.
4、只能調用最新的一條 tweet,剛好滿足我的需求。
5、大概只有國外空間才能使用.(經我驗證,確實如此)
11.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}” ”document.getElementByIdx_x(‘comment’).value += ‘{$value}’”>{$img}</a> ”;
}
echo ‘<div class=”wp_smilies”>’.$link.’</div>’;
}
?>
<?php wp_smilies();?>
將以上代碼複製到 comments.php 中合適的位置。