有時候自己看到一個非常好的網站,但是沒有模板,這個時候該怎么辦呢?其實,在后知的這個博客的邊欄是自己仿出來的,所以這里教大家如何用wp仿站制作模板,后知也是在學習中愿與大家一起學習制作.
首先,我們來進行對wordpress模板結構分析
索引頁模版:index.php
頂部:header.php
文章頁面模板:single.php
邊欄模板:sidebar.php
底部:footer.php
頁面模版:page.php
搜索結果:search.php
文章歸檔:archive.php
評論:comments.php
404 頁面模版:404.php
主題支持函數:functions.php
樣式表:style.css
然后,制作一個可以運行的wordpress主題,修改style.css
制作一個最簡單的主題,只需要兩個文件,index.php和style.css
第一步,準備靜態頁面
第二步,制作index.php和style.css
第三步,給style.css添加版權信息
第四步,把主題上傳到空間中wordpress安裝路徑,wp-content/themes/下面,這里主題的文件夾名字必須是英文
第五步,在wordpress后臺啟用主題
先給style.css添加版權信息
所以的images都要換成<?php bloginfo('template_directory'); ?>/images
Style.css路徑調用:<?php bloginfo( 'stylesheet_url' ); ?>
主題縮略圖名字:screenshot.png
再制作出主題結構,index.php,header.php,footer.php,sidebar.php
把index.php拆分成header.php,footer.php和sidebar.phhp
- <?php get_header();?>
- <?php get_footer();?>
- <?php get_sidebar();?>
還有就是,修改header.php,footer.php
新建一個index.php文件,header.php,footer.php,
這步需要用到的標簽:
獲取博客名字:<?php bloginfo('name'); ?>
獲取博客描述:<?php bloginfo('description'); ?>
獲取主頁路徑:<?php echo get_option('home'); ?>
獲取主題存放路徑:<?php bloginfo('template_directory'); ?>
其他外部文件調用方法:<?php include (TEMPLATEPATH . '/ad/ad2.htm'); ?>
- <meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo( 'charset' ); ?>" />
- <?php wp_head(); ?>
- <title><?php if (is_home()||is_search()) { bloginfo('name'); } else { wp_title(''); print " - "; bloginfo('name'); } ?> </title>
頁面調用:
<?php wp_list_pages('sort_column=menu_order&title_li=&depth=2&include='); ?>
分類目錄調用:
<?php wp_list_categories('title_li=0&orderby=name&show_count=0&depth=2'); ?>
如果當前文章所屬分類為兩個以上還可以格式化輸出它們:
<?php the_category( '-' )?>
這樣就會輸出這樣的內容 “分類1 - 分類2 - 分類3 - ......”
- <p>當前位置: <a href="http://xxx.com/" title="首頁">首頁</a>>><?php $categorys = get_the_category(); $category = $categorys[0];echo(get_category_parents($category->term_id,true,'>>')); ?> 閱讀正文</p>
還有就是跟后知博客的邊欄一樣,制作sidebar.php
最新文章:<?php wp_get_archives('type=postbypost&limit=20'); ?>
日志標題太長超出,修改style.css,用到的代碼:
text-overflow:ellipsis; W*hite-space:nowrap; overflow:hidden;
隨機文章:
- <?php rand_posts as $post) : ?>
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach;?>
不用Random Pages Widget插件實現隨機文章的寫法
- <?php $rand_posts = get_posts('numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?>
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach; ?>
標簽云:<?php wp_tag_cloud('smallest=8&largest=36&'); ?>
文章日期歸檔:<?php wp_get_archives( 'type=monthly' ); ?>
分類目錄:<?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
友情鏈接:<?php wp_list_bookmarks('title_li=&categorize=0&orderby=rand&limit=24'); ?>
友情鏈接調用函數,以下調用類別id為45的友情鏈接
- <?php get_links("45", "<li>", "</li>", "<br>", FALSE, "id", FALSE, FALSE, -1, FALSE);?>
在進行對index.php的制作:
- <?php if (have_posts()) : ?>
- <?php while (have_posts()) : the_post(); ?>
- <?php endwhile; ?>
- <?php else : ?>
- <?php endif; ?>
標題:<a href="<?php the_permalink() ?>"><?php the_title_attribute(); ?></a>
調用文章內容:<?php the_content("Read More..."); ?>
調用文章內容摘要:<?php the_excerpt("Read More..."); ?>
作者:<?php the_author_posts_link(); ?>
日期:<?php the_time('F d, Y') ?>
評論調用:<?php comments_number('No Comment', '1 Comment', '% Comments' );?>
文章所屬分類:標簽:<?php the_category(', ') ?>
上一頁,下一頁調用:
- <div style="float:left"><?php previous_post_link('« %link'); ?></div>
- <div style="float:right"><?php next_post_link('%link »'); ?></div>
最后,制作single.php頁面,加入評論:
在single.php中調用<?php endwhile; ?>和<?php else : ?>中間讓入
<?php comments_template(); ?>
這樣就能夠簡單的制作出來一個模板了,后知愿與大家一起分享.
新聞熱點
疑難解答
圖片精選