This might be a question for Chili-Frie as it's probably specific to the theme, but any ideas would be appreciated. I've had no trouble setting up ZP, but one thing that bothers me is that I cannot display only a partial description sometimes. If you look at one of my subalbums at
http://www.kloofcycles.com/zen/index.php?album=Expedition you will see that some of the descriptions are pretty big. I like having that there when you are on the album page, but when you are just browsing through the albums, it would be nice if there was some way to truncate the amount of description showed (after 40 characters or something). This would keep the page to a more reasonable size and easier to view.
Thanks in advance for your help.
Comments
haven't tried it, but it might work
/tre
- i did try changing the zen.css in the theme folder to set the class "albumdesc" to a specific height, and set overflow:hidden - and that worked, but, it's not elegant at all....bit brutal really - yet in a pinch, it works 4 me.
OTOH - am i missing a bunch of options in my admin page? my version was just installed last night (feb 14) so it must be up-to-date. ???
thanks for being so in touch, this is excellent, i do plan to support you guys in a *real* sense!
again, all the best /tre
in the /zen folder, edit the file: template-functions.php
find the function called printAlbumDesc
right below it, create a new function, as follows:
function printAlbumDescII($editable=false) {
global $_zp_current_album;
if ($editable && zp_loggedin()) {
echo "<div id=\"albumDescEditable\" style=\"display: block;\">" . getAlbumDesc() . "</div>\n";
echo "<script type=\"text/javascript\">initEditableDesc('albumDescEditable');</script>";
} else {
if(strlen(getAlbumDesc())>100) {
echo substr(getAlbumDesc(),0,100) . "...";
} else {
echo getAlbumDesc();
}
}
}
then - in your theme folder, edit the index.php file. where the line is:
<p ><?php printAlbumDesc(); ?>
relace with:
<p ><?php printAlbumDescII(); ?>
- that's it, and it adds an elipses (...) to the end of the cutoff description. in my function above, you see the strlen is checking for strings over 100, otherwise print whole thing. if you want a different length as your cutoff - change in both places.
- i guess to complete this - the cutoff length could be a global, and set it in your config file - but i just started all thi so didn't want to get too carried away.
hth, please let me know if this is not advisable, thanks
/trev
`function truncate_string($string, $length) {
if (strlen($string) > $length) {
$pos = strpos($string, " ", $length);
return substr($string, 0, $pos) . "...";
} else {
return $string;
}
}`
(You don't need to copy it anywhere, it's in functions.php already)
This one truncates a $string to $length, but only does it at word boundaries so things aren't unreadable. Just use it as `truncate_string($description, 100);`
- small issue however, in my test, it does not seem to work....
in my index.php file i have:
<p ><?php truncate_string(printAlbumDesc(),60); ?>
and in my functions.php i changed the function for testing, to this:
function truncate_string($string, $length) {
if (strlen($string) > $length) {
$pos = strpos($string, " ", $length);
return "test no" . substr($string, 0, $pos) . "...";
} else {
return "test yes" . $string ;
}
}
but it prints out the whole description, without truncating, and without pre-pending any text, so it seems like it is not getting called.
so sorry to carry this on one more level, but i'd love to get it right...do you have any advice why this behaves like this?
thanks again! /trev
function printAlbumDescSmall($editable=false) {
global $_zp_current_album;
if ($editable && zp_loggedin()) {
echo "<div id=\"albumDescEditable\" style=\"display: block;\">" . truncate_string(getAlbumDesc(),200) . "</div>\n";
echo "<script type=\"text/javascript\">initEditableDesc('albumDescEditable');</script>";
} else {
echo truncate_string(getAlbumDesc(),200);
}
}
I then call that instead of printAlbumDesc() in index.php and in the subalbums portion of album.php. This worked great for me. Thanks again, both of you.
-Brendan
I have a feeling that this is more complicated than i am imagining.
Either way, good work on these functions.
`function truncate_string($string, $delimiter) {
$pos = strpos($string, $delimiter);
if ($pos === FALSE) return $string;
return substr($string, 0, $pos) . '...';
}`
Or you could edit the printAlbumDesc function (or make a new one)
`function printAlbumDesc($editable=false) {
global $_zp_current_album;
$pos = strpos(getAlbumDesc(), 'xxx')
if ($pos === FALSE) $pos = strlen(getAlbumDesc())
if ($editable && zp_loggedin()) {
echo "
echo "initEditableDesc('albumDescEditable');";
} else {
echo truncate_string(getAlbumDesc(),200);
}
}`
I like the second method better because it doesn't tamper with truncate_string, which is probably used somewhere else. Alternately, you could make a whole new function, like I did and call it instead of printAlbumDesc. You might have to play around with the above script a little as I am not at home so I can't test this and I don't really know php syntax.
Any ideas?
Say the character limit is set to 100 and I want to add a hyperlink into the description.
If the link comes after the first 100 characters everything works great, otherwise it just breaks my html code.
Take this example:
`Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut link`
The first 100 characters are
`Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut `
I there a way to fix this? For example, force the text to be cut only after or before html tags? Or if anyone could come up with a better solution.
`Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut link`
You could do that by simply calling `truncate_string(strip_tags(getAlbumDesc()),200)`.
I don't like this method and I'm assuming that's not what you want either. The other way to do it is to leave the html intact like you were showing above. Here is one way that should work, though it's pretty ugly. Normally, I wouldn't do something like this where it loops through everything, but your descriptions/char limit probably aren't that long anyway so it shouldn't really matter.
`function truncate_string_with_tags($string, $length) {
$pos=0;
$count=0;
$inc=true;
while($pos < $length) {
if($string[$pos] == '<') {<br />
if($string[$pos+1] == '/') {
$count--;
} else {
$count++;
}
$inc=false;
} else if($string[$pos] == '>') {
$inc=true;
}
if($inc) {
$pos++;
}
}
while($count != 0) {
if($string[$pos] == '<') {<br />
if($string[$pos+1] == '/') {
$count--;
} else {
$count++;
}
$inc=false;
} else if($string[$pos] == '>') {
$inc=true;
}
if($inc) {
$pos++;
}
}
return substr($string, 0, $pos) . '...';
}`
Then, just call truncate_string_with_tags() instead of truncate_string() in:
`function printAlbumDescSmall($editable=false) {
global $_zp_current_album;
if ($editable && zp_loggedin()) {
echo "
echo "initEditableDesc('albumDescEditable');";
} else {
echo truncate_string(getAlbumDesc(),200);
}
}`
I haven't tested this and I'm sure there are better ways to do this, but it should at least preserve your links.
I pasted both functions in custom-functions.php and here's what it says when I load tha albums:
Fatal error: Maximum execution time of 60 seconds exceeded in C:\Documents and Settings\PERSONAL\Desktop\xampp\htdocs\zenphoto\themes\default\custom-functions.php on line 9
Line 9 is:
`if($string[$pos+1] == '/') {`
`function truncate_string_with_tags($string, $length) {
if(strlen($string) < $length) {
return $string;
}
$pos=0;
$charcount=0;
$tagcount=0;
$inc=true;
while($charcount < $length) {
if($string[$pos] == '<') {<br />
if($string[$pos+1] == '/') {
$tagcount--;
} else {
$tagcount++;
}
$inc=false;
} else if($string[$pos] == '>') {
$inc=true;
}
if($inc) {
$charcount++;
}
$pos++;
}
while($tagcount != 0 && $pos < $lenth) {
if($string[$pos] == '<') {<br />
if($string[$pos+1] == '/') {
$tagcount--;
} else {
$tagcount++;
}
$inc=false;
} else if($string[$pos] == '>') {
$inc=true;
}
if($inc) {
$pos++;
}
}
return substr($string, 0, $pos) . '...';
}`