Split header and rest of content from content(); tplfunc
-
- Posts: 13
- Joined: Sat Jan 07, 2012 8:37 pm
- Location: Poland
- Contact:
Split header and rest of content from content(); tplfunc
Hi
Is there a chance to get those two as separate functions. This time I need get headers in other place than rest of entry.
Or just some function to return header and I will disable show header in content.
Is there a chance to get those two as separate functions. This time I need get headers in other place than rest of entry.
Or just some function to return header and I will disable show header in content.
Re: Split header and rest of content from content(); tplfunc
Hm, not sure if that could work as expected. You have to consider that content() does not only return the content of a page, but additionally potentially arbitrary output (plugins can manipulate the global $o, and at least for the plugin administration this is the usual technique).
However, all in all I agree, that CMSimple_XH should offer some generally useful functions regarding splitting content in header and "body". There are several routines that need this functionality (in the core and in plugins).
How to get a page heading can be seen in XH_saveEditorContents (cmsimple/admfuncs.php). Basically:
However, all in all I agree, that CMSimple_XH should offer some generally useful functions regarding splitting content in header and "body". There are several routines that need this functionality (in the core and in plugins).
How to get a page heading can be seen in XH_saveEditorContents (cmsimple/admfuncs.php). Basically:
Code: Select all
function getHeading($text)
{
global $cf;
$hot = '<h[1-' . $cf['menu']['levels'] . '][^>]*>';
$hct = '<\/h[1-' . $cf['menu']['levels'] . ']>'; // TODO: use $1 ?
if (preg_match("/$hot(.+?)$hct/isu", $text, $matches)) {
return $matches[1];
} else {
return false;
}
}
Last edited by cmb on Wed May 14, 2014 8:22 pm, edited 2 times in total.
Reason: fixed bugs in code
Reason: fixed bugs in code
Christoph M. Becker – Plugins for CMSimple_XH
-
- Posts: 13
- Joined: Sat Jan 07, 2012 8:37 pm
- Location: Poland
- Contact:
Re: Split header and rest of content from content(); tplfunc
So should I use
and append youre code to adminfuncs? If yes I have error but I will work on it later.
Thanks anyway.
Code: Select all
<?php echo getHeading(content());?>
Thanks anyway.
Re: Split header and rest of content from content(); tplfunc
Basically, yes. I would put the new function into cmsimple/userfuncs.php, however, so it will easily survive an update.
I have just fixed one bug: $cf wasn't "declared" as global. There might be others--I have not tested the code.learnandcode wrote:If yes I have error but I will work on it later.
Christoph M. Becker – Plugins for CMSimple_XH
-
- Posts: 13
- Joined: Sat Jan 07, 2012 8:37 pm
- Location: Poland
- Contact:
Re: Split header and rest of content from content(); tplfunc
Thanks working like a charm.
You forgot the last bracket (for those that only copy and paste)
You forgot the last bracket (for those that only copy and paste)
Re: Split header and rest of content from content(); tplfunc
Fine.learnandcode wrote:Thanks working like a charm.
Oops! Fixed above now.learnandcode wrote:You forgot the last bracket
Christoph M. Becker – Plugins for CMSimple_XH
-
- Posts: 13
- Joined: Sat Jan 07, 2012 8:37 pm
- Location: Poland
- Contact:
Re: Split header and rest of content from content(); tplfunc
Well. I have an issue.
This function working only when i leave heading in content. If I use blank alternative heading I receive void from your function. Any idea?
I cannot leave two h1 cause of crawlers (seo).
This function working only when i leave heading in content. If I use blank alternative heading I receive void from your function. Any idea?
I cannot leave two h1 cause of crawlers (seo).
Re: Split header and rest of content from content(); tplfunc
If you set an alternative heading, the heading is not part of content(), and so getHeading() returns false. You have to leave the heading in (i.e. don't use an empty alternative heading), and use a similar function to remove the heading from content():learnandcode wrote:This function working only when i leave heading in content. If I use blank alternative heading I receive void from your function. Any idea?
Code: Select all
function removeHeading($text)
{
global $cf;
$hot = '<h[1-' . $cf['menu']['levels'] . '][^>]*>';
$hct = '<\/h[1-' . $cf['menu']['levels'] . ']>'; // TODO: use $1 ?
return preg_replace("/$hot(.+?)$hct/isu", '', $text);
}
Code: Select all
<?php echo removeHeading(content());?>
Code: Select all
<?php echo getHeading($cachedContent = content());?>
<!-- further template markup -->
<?php echo removeHeading($cachedContent);?>
I'm not sure if that's really an issue (see http://www.youtube.com/watch?v=GIn5qJKU8VM).learnandcode wrote:I cannot leave two h1 cause of crawlers (seo).
Christoph M. Becker – Plugins for CMSimple_XH