Posts: 43
Threads: 3
Joined: Nov 2012
Hi,
thanks for all the work you're doing to keep this jewel-CMS alive and running.
I'm a php beginner and perhaps my question is just silly. It's about how to recurse a specific text-string in a whole subdirectory.
I have a code like this one:
<?php if (return_page_slug()=='company') { ?><h2>More info</h2><?php } ?>
It returns the text "more info" on the page "company".
But how to make this text appearing also on all the child pages of "company"?
Thanks for your help!
Posts: 3,491
Threads: 106
Joined: Mar 2010
2012-12-13, 01:49:35
(This post was last modified: 2012-12-13, 01:49:55 by Carlos.)
PHP Code: <?php if (get_parent(false)=='company' || return_page_slug()=='company') { ?><h2>More info</h2><?php } ?>
Posts: 43
Threads: 3
Joined: Nov 2012
Posts: 43
Threads: 3
Joined: Nov 2012
Coming back to this topic (I didn't improve my php knowledge during Xmas time...), the solution above works perfectly for child pages but, unfortunately, it fails for nephews.
I was therefore wondering if exists a code useful to overcome the problem and that could be used along all the sub-sub-pages of a specific section?
Thanks in advance for your answers.
Posts: 3,491
Threads: 106
Joined: Mar 2010
2013-01-12, 02:20:41
(This post was last modified: 2013-01-13, 00:57:13 by Carlos.)
Try this:
PHP Code: <?php if ( (get_parent(false)!='' && returnPageField(get_parent(false),'parent')=='company') || get_parent(false)=='company' || return_page_slug()=='company') { ?> <h2>More info</h2> <?php } ?>
If this were to be used for several other pages, not only company, it would be better to create a function for this, of course.
Posts: 43
Threads: 3
Joined: Nov 2012
2013-01-12, 03:09:19
(This post was last modified: 2013-01-12, 03:15:20 by GPB61.)
Thanks Carlos,
I tried but, unfortunately, it didn't work. Nephews are still without a text.
And, indeed, is as you pointed out: I should use this code on different pages, actually one for each section because each section of the site has sub-sub directories within.
Is there a difference if I place the right code onto the template.php (with so many 'if' as the sections of the site) rather than onto functions.php? Why do you think it is better to create a function?
Sorry for this probably silly questions but I'm a real beginner with php...
I forgot to mention tat the wording is different for each section.
So that if in section 'company' the wording is 'More info'', in the section 'products', the wording will be 'List of products'.
This is why I said "as many if as many sections I have"
Posts: 3,491
Threads: 106
Joined: Mar 2010
2013-01-12, 21:06:24
(This post was last modified: 2013-01-12, 23:41:50 by Carlos.)
The snippet works for me. Sub-sub-pages / grandchild pages of company have the text.
[...]
Edit: You were right, it wasn't working for sub-sub-pages. I pasted the wrong code. I've edited the post.
As for using a function, it could be in the template, or in a functions.php file, or even as a small plugin (the latter to allow using it even if you change themes).
But what I mean is you wouldn't have to type (or edit) the slug 3 times. E.g.:
PHP Code: <?php if (page_or_ancestor_is('company')) {?> The conditional text <?php } ?>
Posts: 43
Threads: 3
Joined: Nov 2012
Thank you Carlos,
now it works like a charm...
Actually, I'm a pure web graphic designer and was little in doubt if to use a CMS like GS for the project I'm currently working on. This because there are four languages, hundreds pages, thousands vistors and am a little afraid that the xml structure can break under stress. However, if you don't try you'll never know and this CMS is the only one that allows people with limited knowledge of php to afford such a large project.
You can have a preview from the beta-test site ( http://www.bibibus.com/setec/index.php?i...setlang=en) still largely incomplete but just to give you an idea of where I'm applying your suggestions. Right now, the homepage works for italian language only.
Therefore, every trick to spare time or bytes is welcome. Like for the functions one. The function code you wrote, however, it doesn't apparently work, probably because I'm supposed to use a different wording for 'page_or_ancestor_is' or because I don't actually know where to put the code in the template.
Anyway, this is not a big problem, the important is that the snippet works and thank you very much again for it.
Best!
Posts: 3,491
Threads: 106
Joined: Mar 2010
2013-01-13, 00:58:31
(This post was last modified: 2013-01-13, 01:18:55 by Carlos.)
(2013-01-13, 00:51:18)GPB61 Wrote: Thank you Carlos,
now it works like a charm...
No, it had another bug (giving errors on other pages). I've just edited it again, check it.
page_or_ancestor_is is an imaginary name for an hypothetical function.
But I'll prepare one.
Here it is. Very simple, same as previous snippet, still doesn't work beyond 2 levels (not for great-grandchild pages)
Put this in your theme's functions.php file (create it if doesn't exist)
PHP Code: <?php function page_or_ancestor_is($slug='') { return ( (get_parent(false) != '' && returnPageField(get_parent(false),'parent') == $slug) || get_parent(false) == $slug || return_page_slug() == $slug ); } // end
Usage: in template or component, insert:
PHP Code: <?php if (page_or_ancestor_is('page-slug')) {?> The conditional html/code/text... <?php } ?>
Some suggest a better name for the function? :-)
Posts: 43
Threads: 3
Joined: Nov 2012
Yes! now it is perfect!
Actually, it was perfect also the snippet before but I tested it only on my local server and didn't upload it to the beta-test one... Sorry!
But it is better like we did now, at least I've understood what is a function and how to use it. Even if to be able to write one is another story...
And for the function name I think that "progenitorpage" could be a good one, no? It is little less archaic than ancestor...
Thanks!
Posts: 3,491
Threads: 106
Joined: Mar 2010
As you can see with GetSimple it's rather easy to customize your site with only a bit of simple php.
As for the function name, English is not my native language so I may not choose the appropiate words. But I thought 'progenitor' is the same as 'parent', and doesn't include grandparents.
Posts: 43
Threads: 3
Joined: Nov 2012
English is not even mine native language, but I think that progenitor is someone who originated a stirp. Like, for instance, monkeys (or fish, or reptilians, or God) have generated mankind. Or like Elvis has generated rock'n'roll. In other words someone who stays over the top of something.
And, by the way, GS is a real God(or monkeys, fish, reptilian)'s gift for people like me...
|