主題函式 (functions.php)

主題函式 (functions.php)

Codex Home → BuddyPress Theme Development → functions.php
functions.php

You』ve probably heard a lot of talk about a theme』s functions.php file on the BuddyPress forums or WordPress forums.
So what is it?
functions.php is a file in your theme』s folder where you can add a bunch of custom code hacks and modifications to your theme.
When to use it
functions.php is a sister file to bp-custom.php which may exist in your sites plugin folder. Both files perform a similar type of service, running snippets of code for a site. There are two primary differences in these files though, bp-custom.php runs from the plugins folder and is therefore independent of your theme so is useful for adding snippets that you require to interact with BP regardless of the theme in use also it runs at a specific point in the loading of BP that allows BP to acknowledge it』s instructions, functions.php on the other hand is tied to a specific theme so code in it is only run for that theme, changing themes means those snippets of code will no longer be run unless transfers to the other theme. Choosing which file you need to place your code in is largely a matter of choice, most bp code may be run from function.php and this file generally exists in theme folders already as it may be running WP functions so running your BP code in this file is the usual option and using suitable hooks to ensure your code runs at specific points in the BP process should allow most BP functions to execute but if those do not work or you want your code modifications to be available to any theme then run your code in bp-custom.php.
Creating your file
If you don』t have a file called functions.php in your theme』s folder, go ahead and create one by creating a blank file with the following:
It』s important to note:
that when running child themes, where the parent theme has a functions.php file and your child theme also has one that both files are loaded, the child theme file is checked first (in the same principle that WP manages template hierachy) but then the parent file is run, this is important because if your try and declare the same functions in both you will get a 『Cannot redeclare』 error.
In difference with a normal php file, functions.php must begin with an opening php tag ().
When you encounter a forum thread telling you to put a code snippet in your theme』s functions.php, now you』ll know what to do!
?12<?php// hacks and mods will go here

發表回覆

您的電子郵箱地址不會被公開。 必填項已用 * 標註