Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
component only in child pages
#1
I have a site with two levels of navigation, so just parents and children. I want to insert a component at the bottom of all the child pages only, not the parents.

What's the conditional php I put in my template for that?
Reply
#2
Hmm... There are numerous ways to do it.
You can identify the target page by slug for example:

Code:
if(get_page_slug(false) == 'ma_slug') {
    // show component ...
}

Or, simply check if the current page has a specific parent then show your component:

Code:
$parent = get_parent(false);
if(!empty($parent) && $parent == 'page_parents_slug') {
    // show component ...
}

Or generally, when the same rules apply to all child pages on entire template, you can see whether page has parent then show your component:

Code:
if(!empty(get_parent(false))) {
    // show component ...
}
Reply
#3
(2016-03-04, 16:04:43)Bigin Wrote: ...Or generally, when the same rules apply to all child pages on entire template, you can see whether page has parent then show your component:

Code:
if(!empty(get_parent(false))) {
    // show component ...
}

It's that last one I need, thanks but it is giving me -
Fatal error: Can't use function return value in write context in /var/sites...



Just to be clear I have 
PHP Code:
<?php if(!empty(get_parent(false))) { get_component('tagline')}; ?>
Reply
#4
try this, that should do the trick:
Code:
$parent = get_parent(false);
if(!empty($parent)) {
    // show component ...
}
Reply
#5
(2016-03-05, 03:28:23)Bigin Wrote: try this, that should do the trick:

Code:
$parent = get_parent(false);
if(!empty($parent)) {
   // show component ...
}

For some reason I get the component on all pages with that. I can check the variable is empty with print $parent (and it is). Using
PHP Code:
<?php $parent get_parent(false);
if(!empty(
$parent)) {get_component('disqus');} ?>
Reply
#6
(2016-03-05, 07:19:34)Timbow Wrote: For some reason I get the component on all pages with that. I can check the variable is empty with print $parent (and it is). Using

Because $parent is of type object, you should cast it to a string:

Code:
$parent = (string) get_parent(false);
if(!empty($parent)) {
    ...
}
Reply
#7
Yes!


All working. Smashing. Thanks, mate.
Reply




Users browsing this thread: 1 Guest(s)