1
votes

I need some help tweaking a little PHP for the XTremeCarousel plugin for the Classipress theme for WordPress. This code below truncates the title if it's greater or equal to 14 characters. I need it to do the same thing for the category and the user name so it won't wrap them to another like. If either of them get wrapped, it pushes the 'view more' buttons down below the div they are enclosed in. I can post a link to see the issue if you need it. Here's the part of the code in question, you can see the 2nd line is where the title is grabbed and truncated. I'm going to attempt to rewrite it myself but am open to suggestions as i don't think I have the necessary skills needed to do it ... lol

In the end, each line except the price would need to be truncated to 14 characters so they don't wrap the information down.

<div style="margin-top:16px"></div>
        <a id="rb_title"><?php if ( mb_strlen(get_the_title()) >= 14 ) echo mb_substr( get_the_title(), 0, 14 ).'...'; else the_title(); ?></a>
        <p id="rb_xtrmcarousel">
            Under: <?php if ( get_the_category() ) the_category(', '); else echo get_the_term_list( $post->ID, APP_TAX_CAT, '', ', ', '' ); ?><br />
            By: <span class="owner"><?php the_author_posts_link(); ?></span><br />
            <span class="rb_price">Price: <?php if ( get_post_meta( $post->ID, 'price', true ) ) cp_get_price_legacy($post->ID); else cp_get_price( $post->ID, 'cp_price' ); ?></span>
        </p>

I think this will work for the author's name although it doesn't grab the_author_posts_link.

By: <span class="owner"><?php if ( mb_strlen(get_the_author_nickname()) >= 14 ) echo mb_substr( get_the_author_nickname(), 0, 14 ).'...'; ?><?php if ( mb_strlen(get_the_author_nickname()) <= 14 ) echo mb_substr( get_the_author_nickname(), 0, 14 ).''; ?></span><br />

Every time I try to rewrite the category line, it ends up blank since I don't know what I'm doing... :) however, I can get this to return the category name and it's linked but it's not truncated obviously:

Under: <?php echo get_the_term_list( $post->ID, APP_TAX_CAT, '', ', ', '' ); ?>

Any thoughts?

1
Anybody have any ideas on this? I really need to figure out the proper way to truncate the category like the title is...OcalaDesigns

1 Answers

0
votes

Your problem is that the get_the_term_list function returns an HTML string, so you're counting the characters in the HTML tags as well as those in the category name itself.

Here's some code that should extract the category name from the HTML tags, truncate the name only (if necessary), and output the HTML string with the truncated category name:

Under: 
<?php
    $the_cat_html = get_the_term_list( $post->ID, APP_TAX_CAT);
    preg_match('/<a href="(.+)?">(.+)?</', $the_cat_html, $matches);
    $cat_href = $matches[1];
    $cat_name = $matches[2];
    echo "<a href='" . $cat_href . "'>";
    if ( mb_strlen($cat_name) > 14 )
        echo mb_substr( $cat_name, 0, 14 ) . '...'; 
    else
        echo $cat_name; 
    echo "</a>";
?>

Also, it looks like your code for the author's name will fail if the name has exactly 14 characters. Try this instead:

By: <span class="owner">
<?php
    if ( mb_strlen(get_the_author_nickname()) > 14 )
        echo mb_substr( get_the_author_nickname(), 0, 14 ) . '...'; 
    else
        echo get_the_author_nickname(); 
?>
</span><br />