修改子主題中父主題的行為
什麼是子主題?子主題是一項有用的 WordPress 功能,它允許開發人員無需從頭開始建立新模板,而是利用現有主題中已有的所有功能。
但是,有時,我們為網站選擇的父主題可能具有一些我們不需要的功能(或者我們需要進行自訂以最適合我們的需求),例如自訂貼文類型使用不同的slug、簡碼、JavaScript 函式庫、我們不使用的圖片大小等等...
兩種方式自訂主題
雖然透過直接編輯主題可以輕鬆實現我們想要的目標,但每次更新主題時我們都必須再次進行所有自訂。這可能會令人沮喪,因此還有另一種選擇:我們可以建立一個子主題並使用>functions.php>檔案來修改父主題 > 特點。這樣我們就可以在每次發布新版本時升級父主題,而不會失去我們的自訂內容。
在我們討論更具體的細節之前,先簡單說明主題外觀:我們可以透過匯入子主題的style.css 檔案來修改顏色、背景、排版和佈局父style .css 並覆寫我們想要更改的樣式。
為了對佈局進行更多控制,我們還可以按照 Abbas Suterwala 在他的帖子中的建議,並在子主題中克隆父自訂模板檔案:
然後子主題可以選擇覆寫其他模板文件,例如 author.php、category.php 等。 WordPress 框架首先查找模板檔案在子主題目錄中,如果沒有找到,將從父目錄中取得它。
我們可以修改什麼
透過子主題的functions.php檔案我們可以處理:
- 主題功能
- 自訂貼文類型和分類
- 選單和側邊欄
- 小部件
- 簡碼
- 其他圖像尺寸
- 元框
- JavaScript 與 CSS
- 父主題操作與篩選器
那麼,假設我們有這樣的網站架構:
-
htdocs 或 www
-
#wp-內容
-
主題
-
foo-theme(父主題的目錄 - 它不會被修改)
- functions.php
- ##header.php
- ##style.css
- #其他範本檔案...
-
#foo-theme-child(子主題的目錄)
- functions.php(我們將使用來自訂父主題的檔案)
- header.php(覆寫父主題的header.php)
- style.css(這是子主題中必需的文件,必須命名為style.css)
-
foo-theme(父主題的目錄 - 它不會被修改)
-
主題
-
#wp-內容
讓我們開始吧:在 /wp-content/themes/foo-theme-child/ 目錄中建立一個空的 functions.php 檔案。
在大多數情況下,我們將使用通用的 wp_tuts_remove_features()
函數,掛鉤到 WordPress after_setup_theme
操作。我們也將 10
設定為第三個參數(優先權),因此我們確定該函數在父函數之前觸發。
add_action( 'after_setup_theme', 'remove_parent_theme_features', 10 ); function remove_parent_theme_features() { // our code here }
1.刪除主題功能
一些父主題透過 add_theme_support
函數為 WordPress 新增功能。
可用功能有:
後格式
#後縮圖
#自訂背景
#自訂標頭
#自動提要連結
#所以要刪除它們,我們可以修改functions.php檔案中的remove_parent_theme_features()
函數。
function remove_parent_theme_features() { remove_theme_support( 'post-formats' ); remove_theme_support( 'post-thumbnails' ); remove_theme_support( 'custom-background' ); remove_theme_support( 'custom-header' ); remove_theme_support( 'automatic-feed-links' ); }
2.刪除自訂貼文類型和分類
刪除自訂帖子類型和自訂分類法很簡單:如果父functions.php 檔案透過parent_movie_add_post_type()
函數新增Movie 自訂帖子類型:
// PARENT functions.php add_action( 'after_setup_theme', 'parent_movie_add_post_type' ); function parent_movie_add_post_type() { $parent_args = array( // other arguments... 'rewrite' => array( 'slug' => 'movie' ), 'supports' => array( 'title', 'editor', 'author', 'excerpt' ) ); register_post_type( 'movie', $parent_args ); }
...借助我們的子 functions.php 文件,我們可以自訂它:
// CHILD functions.php function remove_parent_theme_features() { // remove Movie Custom Post Type remove_action( 'init', 'parent_movie_add_post_type' ); /* alternatively, we can add our custom post type to overwrite only some aspects of the parent function */ add_action( 'init', 'child_movie_post_type' ); } function child_movie_post_type() { $child_args = array( // other arguments... // change Custom Post slug 'rewrite' => array( 'slug' => 'child-movie' ), // remove excerpts and add post thumbs 'supports' => array( 'title', 'editor', 'author', 'thumbnail' ) ); register_post_type( 'movie', $child_args ); }
我們也可以在不取消註冊帖子類型的情況下僅刪除某些特徵,例如,如果我們想用帖子特色圖像替換摘錄字段,我們可以這樣修改函數:
function remove_parent_theme_features() { add_action( 'init', 'wp_tuts_remove_post_feature' ); } function wp_tuts_remove_post_feature() { // remove excerpt remove_post_type_support( 'movie', 'excerpt' ); // add post thumbs add_post_type_support( 'movie', 'thumbnail' ); }
您可以在 WordPress Codex 中的 remove_post_type_support
下找到可移除功能的完整清單。
与自定义帖子类型类似,您可以通过 parent_taxonomy()
函数删除在父主题中添加的自定义分类,方法如下:
function wp_tuts_after_setup_theme() { remove_action( 'init', 'parent_taxonomy' ); }
3.删除菜单
我们可以通过 unregister_nav_menu()
函数删除父主题的菜单。该函数采用一个参数,即 register_nav_menu()
函数中使用的菜单位置标识符 slug。
如果父主题注册了标题菜单:
// PARENT functions.php add_action( 'after_setup_theme', 'register_my_menu' ); function register_my_menu() { register_nav_menu( 'header-menu', __( 'Header Menu' ) ); }
我们可以这样删除它:
// CHILD functions.php function remove_parent_theme_features() { unregister_nav_menu( 'header-menu' ); }
要识别已注册的菜单,我们可以在父主题代码中搜索 register_nav_menu()
调用。该函数的第一个参数代表我们可以用来取消注册的菜单 ID(在本例中为 header-menu
)。
4.删除小部件和侧边栏
WordPress 附带了一些我们可以停用的默认小部件。此外,我们的父主题可以添加自己的小部件,因此我们可以在主题文件中搜索它们的声明位置并记下它们的名称。通常它们在扩展 WP_Widget
类的 PHP 类中声明:
// PARENT theme class ParentWidgetName extends WP_Widget { // widget code }
因此,要取消注册小部件,我们使用类名 ParentWidgetName
:
add_action( 'widgets_init', 'wp_tuts_parent_unregister_widgets', 10 ); function wp_tuts_parent_unregister_widgets() { // remove (some) WordPress default Widgets unregister_widget( 'WP_Widget_Pages' ); unregister_widget( 'WP_Widget_Calendar' ); // remove Parent registered Widget unregister_widget( 'ParentWidgetName' ); // register a custom Widget (if needed) register_widget( 'MyCustomWidget' ); } // don't forget to add the Widget Class class MyCustomWidget extends WP_Widget { // Custom Widget code }
对于侧边栏,操作类似:
add_action( 'widgets_init', 'wp_tuts_parent_unregister_sidebars', 10 ); function wp_tuts_parent_unregister_sidebars() { // remove a sidebar registered by the Parent Theme unregister_sidebar( 'first-footer-widget-area' ); }
要识别已注册的侧边栏,我们可以在父主题的代码中搜索 register_sidebar()
调用。
我们需要做的就是记下侧边栏 ID:
// PARENT functions.php $args = array( 'id' => 'first-footer-widget-area', // other args... ); register_sidebar( $args );
5.删除短代码
覆盖或删除短代码很容易,我们只需要这样修改我们的函数:
function remove_parent_theme_features() { // remove the parent [gmap] shortcode remove_shortcode( 'gmap' ); // add our [gmap] shortcode add_shortcode( 'gmap', 'child_shortcode_gmap' ); } function child_shortcode_gmap( $atts ) { // create our shortcode that overwrites the parent one }
要识别已注册的短代码,我们可以在父主题代码中搜索 add_shortcode()
调用。第一个参数是我们要查找的参数;-)。
6.删除附加图像尺寸
如果父主题添加了我们不在子主题中使用的新图像尺寸,我们可以在父主题代码中搜索 add_image_size()
调用。在本例中,它们是:custom_size_parent_1
和 custom_size_parent_2
。我们通过这种方式重置它们:
add_filter( 'intermediate_image_sizes_advanced', 'remove_parent_image_sizes' ); function remove_parent_image_sizes( $sizes ) { unset( $sizes['custom_size_parent_1'] ); unset( $sizes['custom_size_parent_2'] ); return $sizes; }
这很有用,因为每次用户上传图像时,WordPress 都不会创建我们不使用的其他图像尺寸。
要创建自定义图像尺寸,我们可以将其添加到子 functions.php 文件中:
if ( function_exists( 'add_image_size' ) ) { // 400 pixels wide and unlimited height add_image_size( 'custom_size_child_1', 400, 9999 ); // 320 pixels wide and 240 px tall, cropped add_image_size( 'custom_size_child_2', 320, 240, true ); }
7.删除元框
通过 remove_meta_box()
函数,我们可以删除默认的 WordPress 和父主题元框。
WordPress 默认元框列表可在 WordPress Codex 中的 remove_meta_box()
下找到。该函数具有三个参数:元框 ID、将从中删除的页面、编辑上下文(normal
、advanced
、side
)。
如果父主题在帖子编辑屏幕中添加了元框,我们可以通过以下方式禁用它们:
add_action( 'admin_menu' , 'wp_tuts_remove_metaboxes', 99 ); function wp_tuts_remove_metaboxes() { // remove default WP Trackback Metabox from Posts editing page remove_meta_box( 'trackbacksdiv', 'post', 'normal' ); // remove a Parent Theme Metabox 'parent_post_foo_metabox' remove_meta_box( 'parent_post_foo_metabox', 'post', 'normal' ); }
我们可以通过在父主题代码中搜索 add_meta_box
或 add_meta_boxes()
调用来识别父元框。
要删除的元框的 ID 是 add_meta_box()
函数的第一个参数。
8. 删除 JavaScript 和 CSS 样式表
如果父主题添加了我们不需要的 JavaScript 和 CSS 样式:
// PARENT functions.php add_action( 'wp_print_scripts', 'parent_scripts' ); add_action( 'wp_print_styles', 'parent_styles' ); function parent_scripts() { wp_enqueue_script( 'fancybox-parent-js', get_stylesheet_directory_uri() . '/fancybox/jquery.fancybox.pack.js' ); } function parent_styles() { wp_enqueue_style( 'fancybox-parent-css', get_stylesheet_directory_uri() . '/fancybox/jquery.fancybox.css' ); }
我们可以这样删除它们:
// CHILD functions.php add_action( 'wp_print_scripts', 'child_overwrite_scripts', 100 ); add_action( 'wp_print_styles', 'child_overwrite_styles', 100 ); function child_overwrite_scripts() { wp_deregister_script( 'fancybox-parent-js' ); } function child_overwrite_styles() { wp_deregister_style( 'fancybox-parent-css' ); }
要识别已注册的 JavaScript 和 CSS 样式,我们可以在父主题代码中搜索 wp_enqueue_script()
和 wp_enqueue_style()
调用。
该函数的第一个参数是我们可以在 wp_deregister_script()
或 wp_deregister_style()
函数中使用的参数。
9.删除父主题操作和过滤器
某些主题(例如 Thematic)提供了多个挂钩来修改主题行为,而无需更改主题文件。在本例中,Thematic 提供了一个 thematic_header
操作来加载其他操作:
thematic_brandingopen()
thematic_blogtitle()
thematic_blogdescription()
thematic_brandingclose()
thematic_access()
我们不会详细研究这些函数的作用,可能其中一些函数会在博客标题中打印一些信息:名称、描述等等...在这种情况下,我们可以停用 thematic_blogdescription()
函数这样:
// Unhook default Thematic functions function unhook_thematic_functions() { // we put the position number of the original function (5) // for priority reasons remove_action( 'thematic_header', 'thematic_blogdescription', 5 ); } add_action( 'init', 'unhook_thematic_functions' );
在这些情况下,可能很难理解父主题的结构及其工作原理。我的建议是选择一个带有详细文档、良好的支持论坛并在整个代码中提供钩子的父主题。
这肯定会让我们损失更少的开发时间,并使子主题的定制变得更加容易。
參考文獻
- 子主題基礎知識和在 WordPress 中建立子主題
-
WordPress Codex 文件
after_setup_theme
remove_action
#add_theme_support
register_post_type
add_post_type_support
remove_post_type_support
#register_nav_menu
unregister_nav_menu
register_widget
unregister_widget
register_sidebar
#unregister_sidebar
add_shortcode
#remove_shortcode
add_image_size
add_meta_box
remove_meta_box
wp_deregister_script
wp_deregister_style
- WordPress 父主題集合
- 專題主題架構
以上是修改子主題中父主題的行為的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。
