学习笔记5——wp主题开发
我覺得學(xué)習(xí)wordpress插件開發(fā)之前還是得先理解一下wp的主題開發(fā),循序漸進才能學(xué)好wordpress開發(fā),話不多說,接下來整理一下這兩天學(xué)習(xí)的wordpress主題開發(fā)的一些心得和體會,與大家一起分享,如果有哪些地方存在異議,敬請指出!
先簡單的介紹一下什么是主題,wp的主題就是你的wp主頁顯示的內(nèi)容,你可以自定義你的wp頁面以什么樣的形式去顯示給用戶看,其實跟平時我們自己開發(fā)網(wǎng)站沒什么兩樣,都是得自己寫css、html、php代碼,區(qū)別就是可以使用wp內(nèi)部自定義的函數(shù),wp內(nèi)部封裝很多函數(shù)供我們使用,還有wp內(nèi)部內(nèi)定了很多模版,只要在你的主題下面按照wp規(guī)定命名一些文件,wp都會智能地去調(diào)用,例如404.php,header.php等等,接下來我也會詳細介紹這些文件在wp中的命名規(guī)則。
首先簡單介紹一下wp數(shù)據(jù)庫表結(jié)構(gòu):
wp_comments表:存儲了wp的所有評論內(nèi)容;
wp_commentmeta表:評論元數(shù)據(jù)表,存儲了評論的一些附加信息;
wp_links表:存儲了wp網(wǎng)站的所有鏈接;
wp_options表:存儲了wp中的基本信息;
wp_posts表:存儲了wp中上傳的文章信息;
wp_postmetab表:存儲了文章的元信息;
wp_terms表:存儲了wp中的分類信息;
wp_termmeta表:存儲了wp中的分類元信息;
wp_users表:存儲了wp中的用戶信息;
wp_usermeta表:存儲了wp中的用戶元信息;
wp的主題模版是如何加載的呢?
wp一開始會進入index.php文件,
然后載入wp-blog-header.php,在此文件中進行數(shù)據(jù)庫等初始化過程,接下來會調(diào)用template-loader.php進行模版的載入,也就是載入/wrdpress/wp-content/themes/文件夾里面的模板。所以每次你進入wp,看到的首頁其實就是themes目錄下其中一個模板的index.php頁面。
主題模板的創(chuàng)建和安裝?
主題模板的創(chuàng)建就是在/wp_content/themes/下創(chuàng)建一個文件夾my-themes(名稱你自己決定),在新文件夾下新建一個style.css文件,在css文件的開頭處加上一段包含Theme Name: my-themes的注釋,接下來你可以選擇一張圖片作為你的themes的圖片,有個要求就是圖片名稱必須命名為screenshot.png,這樣wp才能識別出來(接下來會有很多文件的創(chuàng)建也是必須指定文件名),然后保存,打開你的儀表盤頁面下的外觀-主題,你會看到你剛剛創(chuàng)建的主題模板。
/*
Theme Name: Ryan.Zheng
Theme URI: https://wordpress.org/themes/twentyfifteen/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Our 2015 default theme is clean, blog-focused, and designed for clarity. Twenty Fifteen's simple, straightforward typography is readable on a wide variety of screen sizes, and suitable for multiple languages. We designed it using a mobile-first approach, meaning your content takes center-stage, regardless of whether your visitors arrive by smartphone, tablet, laptop, or desktop computer.
Version: 1.8
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: blog, two-columns, left-sidebar, accessibility-ready, custom-background, custom-colors, custom-header, custom-logo, custom-menu, editor-style, featured-images, microformats, post-formats, rtl-language-support, sticky-post, threaded-comments, translation-ready
Text Domain: twentyfifteen
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/
/*#header{
background: green;
}*/
頂部和底部模板的調(diào)用:
你可以在你新建的模板文件夾my-themes下,新建兩個文件header.php和footer.php,wp會默認這兩個文件為模版的頭部文件和底部文件,然后將index.php文件中的頭部代碼和底部代碼分別存放于這兩個文件中。你可以在index.php中通過get_header()和get_footer()這兩個函數(shù)久可以直接header.php和footer.php文件中的代碼,是不是很方便!相比之前的php代碼,我們的做法是通過include 'header.php';這種格式去調(diào)用。下圖是wp默認的一些文件的命名,
如何才能調(diào)用數(shù)據(jù)庫里的數(shù)據(jù)?
一開始我們的想法可能是通過pdo連接數(shù)據(jù)庫,然后各種query();的方式去處理,wp為我們提供了很多函數(shù),你可以直接通過一個函數(shù)獲取帶你想要的數(shù)據(jù)庫數(shù)據(jù),接下來我會介紹一下如何調(diào)用wp_options表的數(shù)據(jù),其他的大家觸類旁通。
wp_options表存放了wp網(wǎng)站的一些基本的信息,例如博客名稱,網(wǎng)站地址,編碼等等,你可以通過get_option(鍵名)獲取wp_option表的數(shù)據(jù),你也可以通過update_option(鍵名,新的鍵值)函數(shù)修改鍵值。
ps:數(shù)據(jù)表中鍵名以_開頭的都是隱藏的,也就是不會顯示在后臺儀表盤頁面上給用戶看到。
總結(jié)
以上是生活随笔為你收集整理的学习笔记5——wp主题开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言中指针与取地址符&详解
- 下一篇: 栀子花的养殖方法(栀子花的种植方法和注意