-
Notifications
You must be signed in to change notification settings - Fork 5
Components
Components are easily editable containers for pieces of code or text that can be inserted into a theme template. They are very adaptable and have many uses. At their simplest, components are ideal for small strings of text that are part of a theme file that the website owner may want to edit down the road. Examples are taglines, quotes, or sidebar items. But also:
- Components can be used to edit and insert whole blocks of formatted html content into a sidebar, a footer or a multi-column layout.
- Components can contain code so they can be used to insert dynamic content like a gallery, a list of blog posts, an image slider or a contact form.
- Components can be included in all pages or individual pages or only in specific pages by conditional statements in the template. See Components Depending On The Page.
- Components can be used to alter items in the head section of a page, for example to add css or a robots.txt
- Components can be used inside other components
- Components can be saved empty, with no content
To create a component, log into your administrator panel and goto Theme → Edit Components. From there, it is pretty self explanatory on how to add or edit existing components.
At the top right of each component is the PHP coded needed to insert that particular component into a theme file:
<?php get_component('tagline'); ?>
Hint: To change an existing component's title, double click the title, and it will change to become editable.
If you would like a component to be displayed on only one page, that can be done by adding the following code to your theme's template.php file:
<?php if (return_page_slug()=='PAGE') get_component('COMPNAME'); ?>
Don't forget to replace PAGE with your page's slug and COMPNAME with the component's name.
Say you have three pages with the slugs index, about, contact and sidebars as components named peter, paul and mary you would edit your template to replace
<?php get_component(sidebar); ?>
with
<?php if (return_page_slug()=='index') {get_component('peter');}
elseif (return_page_slug()=='about') {get_component('paul');}
else {get_component('mary');} ?>
which in English reads
If the page slug is index insert the component peter, otherwise if the page slug is about insert the component paul and otherwise just insert the component mary.
Now If you want a component to be displayed on pages of a specific parent page, you can do it in a similar way:
<?php if (get_parent(0)=='PAGE') get_component('COMPNAME'); ?>
Again, don't forget to replace PAGE with your page's slug and COMPNAME with the component's name.
If you would like to exclude a component from a page you could do that by replacing “==” with “!=” in the code above.
The Components might contain a header image, sidebar content, tagline text, anything. If you only want one page to be different it might be simplest to just make a duplicate page template, but if you want them different on each of several pages here is how.
The following text needs to be in a functions.php file in your theme folder:
<?php
if (('component_exists')) {
function component_exists($id) {
global $components;
if (!$components) {
if ([file_exists](http://www.php.net/file_exists)(GSDATAOTHERPATH.'components.xml')) {
$data = getXML(GSDATAOTHERPATH.'components.xml');
$components = $data->item;
} else {
$components = [array](http://www.php.net/array)();
}
}
$exists = FALSE;
if ([count](http://www.php.net/count)($components) > 0) {
foreach ($components as $component) {
if ($id == $component->slug) {
$exists = TRUE;
break;
}
}
}
return $exists;
}
}
?>
Make a component which will be the default component and call it say default. Make components for specific pages and name them to include the page slug of the page into which they are to be inserted, so name them say content-about and content-contact. Then call the component in your page template as follows:
<?php if (component_exists('content-'.get_page_slug(false)))
{get_component('content-'.get_page_slug(false));}
else {get_component('default');} ?>
🏠 Home
⚙️ Installation
👷 Admin Reference
📝 Adding and Editing Content
- WYSIWYG Editor
- Components
- Snippets
- Custom 404 Page
🎨 Themes
- Theme Installation
- Theme Creation
- Step-by-Step Tutorial
- Template functions
- Template Tags
- Template Code Snippets
- Partial Template Files
🔌 Plugins
- Plugin Installation
- Plugin Creation
- Plugin functions
- Plugin Hooks & Filters
- Using Tabs and Sidebar Menus
- Tips & Tricks
💪 Advanced