How to shrink album descriptions

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

  • I am not sure there is a function for this yet in zenphoto. I will take a look and see what I can find but I am pretty sure you would have to write a custom function to attain this.
  • If you have any hints on where to start with that, I'm more than willing to take a crack at it.
  • maybe on the index page, you can wrap the description in a smaller container div, and set overflow to none.....just a thought. maybe put a "..." as a footer too.

    haven't tried it, but it might work
    /tre
  • trisweb Administrator
    There is a function, as a matter of fact, it's used in the comment display on the admin page. It's probably called truncate_something... I'll find it later if you can't.
  • hi - i am not disagreeing - but i don't see that function anywhere? sorry for seeming to disagree with you <geez!>

    - 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
  • ok...i see the view truncated/full remark in the coments section, but i believe that i, and elderz were looking for something a bit different - so i submit this for everyones review:

    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
  • trisweb Administrator
    Looks good :-) Like I said though, there's already a function in ZP for that:

    `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);`
  • thanks - that is clearly the correct method and i need to study all the functions so i know whats there already :)

    - 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
  • Hi guys. Thanks for all your help. I haven't actually taken a look at zp in a while as I've been busy with some other projects (you know how it is). Thanks for pointing out the existing function Tris. trehug is right in that we were trying to have the description only truncate on the album select page (index or subalbum page). For that, I just made a second function printAlbumDescSmall():

    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
  • tcserpa Member
    I am using this (wonderful) technique to limit project descriptions, and now i have a question. Would it be possible to set a character, or other specific string as the truncate location? As in, rather than truncating at the 100th character it would truncate at the first instance of 'xxx'?

    I have a feeling that this is more complicated than i am imagining.

    Either way, good work on these functions.
  • elderz Member
    There are a couple different ways you can do this I think. First would be to alter the truncate_string function. You could do something like

    `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 "
    " . truncate_string(getAlbumDesc(),$pos) . "
    n";

    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.
  • elderz - For some reason, I can't get this to work. I'm a bit of a noob. I copied and pasted the function exactly as you have it into the template-functions.php and called it in the index.php with printAlbumDescSmall()

    Any ideas?
  • Never mind, I'm an idiot.
  • You want to put it in album.php when you call that though you probably already figured that out. Don't worry, we've all done that (and will again).
  • I have a problem with this code.
    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.
  • There are generally two schools of thought on this. The first is that you enforce the character limit strictly and just strip tags from the content so it would come out as plain text, looking something like:

    `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 "
    " . truncate_string(getAlbumDesc(),200) . "
    n";

    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.
  • Thanks elderz. There's something that prevents the code from working though.
    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] == '/') {`
  • Edit: I overlooked a couple things. This one works for me:

    `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) . '...';

    }`
  • Works like a charm now. Thank you!
Sign In or Register to comment.