|
復(fù)制代碼 代碼如下:
<?php
/*
在根目錄 -> wp-content -> themes 下創(chuàng)建mytheme文件夾用來存放創(chuàng)建新主題模板
在mytheme目錄下創(chuàng)建 index.php ,style.css 兩個(gè)文件,在wp后臺(tái) 外觀->主題 中就可以看到剛創(chuàng)建的主題
打開style.css文件輸入
*/
?>
/*
Theme Name: 這里填主題名稱
Theme URI: 這里填主題介紹的網(wǎng)址,沒有就填你的博客網(wǎng)址吧
Description:這里填主題的簡短介紹
Author: 作者名
Author URI: 作者的網(wǎng)址
Version: 版本號(hào)
Tags: 標(biāo)簽,多個(gè)用半角逗號(hào)隔開
*/
<?php
/*
在后臺(tái)主題管理中即可看到主題相關(guān)信息,css中主題信息內(nèi)容必須用注釋符號(hào)括起來
找一個(gè)300*225的png圖片,命名為 screenshot.png 放在主題目錄下(mytheme文件夾中),在主題管理頁中即可看到新建主題的預(yù)覽圖片
//==================================================header================================================================
可以把網(wǎng)站相同頭內(nèi)容放在一個(gè)頭文件中,在主題目錄下新建 header.php 文件向其中輸入輸入 統(tǒng)一的頭部內(nèi)容
在 index.php 或想調(diào)用該header.php頁面的頁面中 輸入
*/
get_header(); //get_header()就相當(dāng)于將header.php中的代碼拷貝到當(dāng)前的php文件
/*
在主題管理頁面,該主題實(shí)時(shí)預(yù)覽中,默認(rèn)打開的 index.php 頁面中即可引入 header.php 頁面的內(nèi)容
header.php 將會(huì)被所有的模板頁面(主頁、分類頁、頁面、標(biāo)簽頁等)所包含,所以 header.php 中代碼應(yīng)該是動(dòng)態(tài)的。
不同頁面的title都是不一樣,而且title的設(shè)置還會(huì)直接影響到SEO的效果,所以這里應(yīng)該謹(jǐn)慎設(shè)置。下面提供一種SEO優(yōu)化的title寫法,
在header.php頁面添加
*/
?>
<title>
<?php
if (is_home ()) { // is_home() 當(dāng)前頁面為主頁時(shí)返回true
bloginfo ( 'name' ); // 返回站點(diǎn)標(biāo)題
echo " - ";
bloginfo ( 'description' ); // 返回站點(diǎn)副標(biāo)題,站點(diǎn)描述
} elseif (is_category ()) { // is_category() 當(dāng)前頁面為分類頁時(shí)返回true
single_cat_title ();
echo " - ";
bloginfo ( 'name' );
} elseif (is_single () || is_page ()) { // is_single() 當(dāng)前頁面為單文章頁時(shí)返回true 。 is_page() 當(dāng)前頁面為單頁面時(shí)返回true
single_post_title ();
} elseif (is_search ()) { // is_search() 當(dāng)前頁面為搜索頁時(shí)返回true
echo "搜索結(jié)果";
echo " - ";
bloginfo ( 'name' );
} elseif (is_404 ()) { // is_404() 當(dāng)前頁面為404頁時(shí)返回true
echo '頁面未找到!';
} else {
wp_title ( '', true );
}
?>
</title>
<?php
/*
以上添加的php代碼運(yùn)用了條件判斷,針對(duì)不同的頁面采用不同title
在 header.php 頁面中添加默認(rèn) style.css 文件
*/
?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php
/*
bloginfo('stylesheet_url');返回的是主題默認(rèn)style.css文件絕對(duì)網(wǎng)址路徑,如
http://localhost/wordpress/wp-content/themes/myTheme/style.css
bloginfo('template_url');返回的是主題目錄的絕對(duì)網(wǎng)址路徑,可以用來模板中連接樣式圖片,如
http://localhost/wordpress/wp-content/themes/mytheme
添加 pingback 通告功能,在header.php頁面 <head> 標(biāo)簽中里面添加代碼:
*/
?>
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<?php
/*
添加訂閱feed鏈接,在header.php頁面 <head> 標(biāo)簽中添加:
*/
?>
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有文章" href="<?php echo get_bloginfo('rss2_url'); ?>" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有評(píng)論" href="<?php bloginfo('comments_rss2_url'); ?>" />
<?php
/*
添加wp_head,有些插件需要在網(wǎng)頁頭部添加一些js或css,要讓這些插件能夠正常的工作,也讓主題有更好的兼容性,應(yīng)該添加wp_head()函數(shù)
header.php 頁面 <head> 標(biāo)簽中添加
*/
?>
<?php wp_head(); //用于包含WordPress程序輸出頭部信息 ?>
<?php
/*
顯示菜單欄,這里只在菜單欄中列出分類頁和page頁面,可以根據(jù)喜好來列出想要的。header.php中
*/
?>
<ul id="navigation" class="grid_8">
<?php wp_list_categories(); //用于列出博客分類頁 ?>
<?php wp_list_pages('depth=1&title_li=0&sort_column=menu_order'); //用于列出博客頁面,可不填參數(shù) ?>
</ul>
<?php
//==================================================footer================================================================
/*
footer.php與header.php差不多,寫這個(gè)文件的目的也是為了精簡代碼,提高代碼的重用性。
在主題目錄中創(chuàng)建 footer.php ,在 index.php 或想調(diào)用該footer.php頁面的頁面中使用
*/
get_footer();//功能和get_header()類似
/*
在footer.php頁面添加 wp_footer提高兼容性
*/
wp_footer();
/*
wp_footer()和wp_head()差不多,都是用于提高主題兼容性,畢竟有很多插件要在頁腳輸出一些東西才能正常工作。
*/
//==================================================sidebar================================================================
/*
在主題目錄下新建 sidebar.php 頁面,在 index.php 或想調(diào)用該sidebar.php頁面的頁面中添加
*/
get_sidebar();
/*
調(diào)用 sidebar.php 頁面內(nèi)容
為使WordPress后臺(tái) -> 外觀 -> 小工具,可以正常地拖動(dòng)小工具到側(cè)邊欄
在 sidebar.php 頁面的列表格式應(yīng)按如下舉例格式
*/
?>
<div>
<?php
if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'First_sidebar' )) ://First_sidebar為widget名稱,要和functions.php中對(duì)應(yīng)的widget name相同
?>
<h4>分類目錄</h4>
<ul>
<?php wp_list_categories('depth=1&title_li=&orderby=id&show_count=0&hide_empty=1&child_of=0'); ?>
</ul>
<?php endif; ?>
<?php
if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'Second_sidebar' )) :
?>
<h4>最新文章</h4>
<ul>
<?php
$posts = get_posts ( 'numberposts=6&orderby=post_date' );
foreach ( $posts as $post ) {
setup_postdata ( $post );
echo '<li><a href="' . get_permalink () . '">' . get_the_title () . '</a></li>';
}
$post = $posts [0];
?>
</ul>
<?php endif; ?>
<?php
if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'Third_sidebar' )) :
?>
<h4>標(biāo)簽云</h4>
<p><?php wp_tag_cloud('smallest=8&largest=22'); ?></p>
<?php endif; ?>
<?php
if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'Fourth_sidebar' )) :
?>
<h4>文章存檔</h4>
<ul>
<?php wp_get_archives('limit=10'); ?>
</ul>
<?php endif; ?>
</div>
<?php
/*
同時(shí)在主題目錄下創(chuàng)建 functions.php 文件內(nèi)容為
*/
/** widgets */
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'First_sidebar', //name就是給widget指定各自的名稱,以便在sidebar.php中分別調(diào)用.所以只需要給這兩個(gè)widget取兩個(gè)名字就好了。
'before_widget' => '', //定義Widget內(nèi)容的前后標(biāo)識(shí)符的語句
'after_widget' => '',
'before_title' => '<h4>', //定義Widget標(biāo)題的前后標(biāo)識(shí)符的語句
'after_title' => '</h4>'
));
register_sidebar(array(
'name' => 'Second_sidebar',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h4>',
'after_title' => '</h4>'
));
register_sidebar(array(
'name' => 'Third_sidebar',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h4>',
'after_title' => '</h4>'
));
register_sidebar(array(
'name' => 'Fourth_sidebar',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h4>',
'after_title' => '</h4>'
));
}
/*
這樣WordPress后臺(tái) -> 外觀 -> 小工具,就可以正常地拖動(dòng)小工具到側(cè)邊欄了
制作index.php 文章列表
例子
*/
?>
<div class="grid_8">
<!-- Blog Post -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<!-- Post Title -->
<h3 class="title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<!-- Post Data -->
<p class="sub"><?php the_tags('標(biāo)簽:', ', ', ''); ?> • <?php the_time('Y年n月j日') ?> • <?php comments_popup_link('0 條評(píng)論', '1 條評(píng)論', '% 條評(píng)論', '', '評(píng)論已關(guān)閉'); ?><?php edit_post_link('編輯', ' • ', ''); ?></p>
<div class="hr dotted clearfix"> </div>
<!-- Post Image -->
<img class="thumb" alt="" src="<?php bloginfo('template_url'); ?>/images/610x150.gif" />
<!-- Post Content -->
<?php //the_excerpt(); ?>
<?php the_content('閱讀全文...'); ?>
<!-- Read More Button -->
<p class="clearfix"><a href="<?php the_permalink(); ?>" class="button right">閱讀全文</a></p>
</div>
<div class="hr clearfix"> </div>
<?php endwhile; ?>
<!-- Blog Navigation -->
<p class="clearfix"><?php previous_posts_link('<< 查看新文章', 0); ?> <span class="float right"><?php next_posts_link('查看舊文章 >>', 0); ?></span></p>
<?php else : ?>
<h3 class="title"><a href="#" rel="bookmark">未找到</a></h3>
<p>沒有找到任何文章!</p>
<?php endif; ?>
</div>
<?php
/*
have_posts(); 判斷是否有下一個(gè)文章
the_post(); 改變當(dāng)前文章指向到下一個(gè)文章
the_permalink(); 當(dāng)前指向文章的連接地址
the_title(); 當(dāng)前指向文章的標(biāo)題
the_tags('標(biāo)簽:'); 當(dāng)前指向文章的標(biāo)簽
comments_popup_link('0 條評(píng)論', '1 條評(píng)論', '% 條評(píng)論', '', '評(píng)論已關(guān)閉'); 顯示打印當(dāng)前指向文章的評(píng)論鏈接
edit_post_link('編輯', ' • ', ''); 當(dāng)前指向文章,顯示打印當(dāng)前指向文章的編輯鏈接
the_excerpt(); 當(dāng)前指向文章,只要在寫文章的時(shí)候在"摘要"框內(nèi)填寫摘要,在首頁顯示的就是摘要,如果不填就輸出全文!
the_content('閱讀全文...'); 用于輸出當(dāng)前指向文章全文,除非在文章中使用了<!-- more -->
the_permalink(); 返回當(dāng)前指向文章閱讀全文的連接地址
previous_posts_link('<< 查看新文章', 0); 顯示打印當(dāng)前顯示列表分頁連接(每頁文章數(shù)量取決于在后臺(tái)設(shè)置每頁可顯示的文章數(shù)量)
next_posts_link('查看舊文章 >>', 0); 顯示打印當(dāng)前顯示列表分頁連接
the_time('Y年n月j日');顯示日期如 1999年5月1日
另外,還有個(gè)存檔頁面的模板archive.php,跟index.php的制作過程完全一樣,只不過需要在functions.php里添加一個(gè)函數(shù)
單文章頁single.php,可以根據(jù)index.php頁往這里添加自己想要顯示的內(nèi)容
page.php 也就是頁面,博客上的所有網(wǎng)頁都是頁面,這里指的頁面一個(gè)單獨(dú)的頁面,如"關(guān)于"、"聯(lián)系方式"等,可以在WordPress后臺(tái) 主站蜘蛛池模板: 宁晋县| 铁力市| 五莲县| 定襄县| 青海省| 滦平县| 封丘县| 邳州市| 石门县| 准格尔旗| 南靖县| 莒南县| 乌审旗| 贵州省| 景东| 咸阳市| 紫金县| 阿克苏市| 景东| 体育| 大连市| 平谷区| 辉县市| 祁门县| 界首市| 射阳县| 乌拉特中旗| 富源县| 西平县| 石楼县| 德安县| 景泰县| 乌鲁木齐县| 迭部县| 青阳县| 新野县| 景宁| 镇赉县| 炉霍县| 贡觉县| 成安县|