Posts: 57
Threads: 2
Joined: Feb 2015
Hello everybody,
I would like to extend my if/then statement with an argument whether the page is set to private or not. Its content shouldn't be shown if its private.
How should I add that part to this existing code?
PHP Code:
if (!empty(returnPageContent('aktion'))) { ?>
Thank you very much in advance.
Posts: 538
Threads: 12
Joined: May 2013
2017-09-20, 00:34:04
(This post was last modified: 2017-09-20, 00:44:53 by Bigin.)
Hi smsHH,
you could use nested function call to achieve this, something like:
PHP Code:
if(empty(returnPageContent('aktion', 'private'))) {
if(!empty(returnPageContent('aktion'))) {
...
}
}
However, it is not optimal, because the file has to be read 2 times. A better option would be to write a simple custom function for this purpose:
PHP Code:
function getPublicPageContent($slug) {
$data = getXml(GSDATAPAGESPATH.$slug.'.xml');
if(isset($data) && empty($data->private)) {
return exec_filter('content', stripslashes(htmlspecialchars_decode($data->content, ENT_QUOTES)));
}
}
You can then call this function as follows:
PHP Code:
echo getPublicPageContent('aktion');
;-)
Posts: 6,267
Threads: 182
Joined: Sep 2011
use getPageField for private checking, it uses pagecache in bigin s first example instead of returnPageContent
Posts: 538
Threads: 12
Joined: May 2013
2017-09-20, 01:12:41
(This post was last modified: 2017-09-20, 01:21:36 by Bigin.)
Yes, you could also use getPageField. But I think, in this case, it makes not really sense to use page cache, if only one page needs to be checked. Page cache makes sense only if $pagesArray already in memory and several pages should to be checked within a loop, for example. Or did I misunderstood something there?
Posts: 57
Threads: 2
Joined: Feb 2015
Thank you both, I will try that (and try to understand the code). :-)
Posts: 57
Threads: 2
Joined: Feb 2015
Great, it works. Thank you again!
A small question though. I need to wrap the content into some HTML. How can I achieve that the markup will be shown only if the page isn't set to private (or/and has content), too?
PHP Code:
<!-- Start Aktion -->
<div id="aktion">
<?php echo getPublicPageContent('aktion'); ?>
<div class="closeaktion" title="Fenster schließen">×</div>
</div>
<!-- Ende Aktion -->
Thank you :-)
Posts: 538
Threads: 12
Joined: May 2013
Well, you could extend your
getPublicPageContent() function before it returns the page content with the markup:
PHP Code:
...
if(isset($data) && empty($data->private)) {
$content =
'<div id="aktion">'.
exec_filter('content', stripslashes(htmlspecialchars_decode($data->content, ENT_QUOTES))).
'<div class="closeaktion" title="Fenster schließen">×</div>'.
'</div>';
return $content;
}
Posts: 57
Threads: 2
Joined: Feb 2015
Simply amazing
Thank you so much, Bigin!
Posts: 6,267
Threads: 182
Joined: Sep 2011
for future reference yeah only use page cache when you need it, if you need info on more than one page etc, but as of 3.3.x I think its always loaded, its not lazy until 3.4, where do you think the menu comes from ?
Posts: 538
Threads: 12
Joined: May 2013
Oh yes, that's right, the menu ... then $pagesArray has probably already been loaded