2012-02-11 14 views
1

我需要一些帮助,为WordPress的Classipress主题调整XTremeCarousel插件的一个小小的PHP。如果该标题大于或等于14个字符,则下面的代码将截断标题。我需要它为类别和用户名做同样的事情,所以它不会把它们包装到另一个类似的地方。如果他们中的任何一个被封装,它会将“查看更多”按钮放在它们所在的div下方。如果需要,我可以发布链接查看问题。以下是有问题的代码部分,你可以看到第二行是标题被截取的地方。我会尝试自己重写它,但我愿意提供建议,因为我认为我没有必要的技能来做到这一点......大声笑抓取WordPress分类并截断它

最后,除了价格之外的每一行都需要被截断为14个字符,因此它们不包含信息。

<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> 

我认为这将适用于作者的名字,虽然它不抓住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 /> 

每次我尝试重写类线,它结束了空白,因为我不知道我在做什么... :)不过,我能得到这个返回的类别名称和它的但它并没有被明显截断:

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

有什么想法?

+0

有人有什么想法吗?我真的需要找出正确的方法来截断类别,如标题是... – OcalaDesigns 2012-02-16 16:04:36

回答

0

您的问题是get_the_term_list函数返回一个HTML字符串,因此您要计算HTML标记中的字符以及类别名称本身中的字符。

下面是一些代码,应该从HTML标签提取类别名称,只截断名称(如果需要),并输出HTML字符串截断类别名称:

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>"; 
?> 

此外,它看起来像如果名称正好有14个字符,则作者姓名的代码将失败。试试这个:

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 />