User Tools

Site Tools


components-depending-on-the_page

This is an old revision of the document!


Components Depending On The Page

Component Only on One Page

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.

Components Depending On The Parent Page

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.

Exluding components

If you would like to exclude a component from a page you could do that by replacing==” with “!=” in the code above.

A System for Different Components on Different Pages

The Components might contain a header image, sidebar content, tagline text, anything. If you only want one page to be different it would 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:

<?php
if (!function_exists('component_exists')) {
    function component_exists($id) {
        global $components;
        if (!$components) {
             if (file_exists(GSDATAOTHERPATH.'components.xml')) {
                $data = getXML(GSDATAOTHERPATH.'components.xml');
                $components = $data->item;
            } else {
                $components = array();
            }
        }
        $exists = FALSE;
        if (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 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');}	?>

Links

How to Have Different sidebars

components-depending-on-the_page.1365464467.txt.gz · Last modified: 2013/04/19 14:53 (external edit)