[Giúp đỡ] Các bạn giúp mình về cắt chuỗi kí tự title trong joomla 1.5.x

Thảo luận trong 'Joomla Code Joomla! 1.5.x' bắt đầu bởi jelyo, 27/2/12.

  1. jelyo

    jelyo Thành viên mới

    Các bạn giúp mình với.
    đây là file "blog_item.php" trong joomla (com_content\section) của mình.
    mình đang gặp vấn đề về cắt bớt chuỗi kí tự title của bài viết.
    mình muốn giới hạn kí tự title của bài viết khoảng 50 kí tự rồi hiển thị phần còn lại thành dấu "...".
    vd title bài viết: Biệt thự mang phong cách tân cổ điển. thì mình muốn nó thành thế này Biệt thự mang phong cách...
    rất mong được sự giúp đỡ của các bạn. Thanks you so much!

    PHP:
    <?php // no direct access
    defined('_JEXEC') or die('Restricted access'); ?>
    <?php $canEdit   
    = ($this->user->authorize('com_content''edit''content''all') || $this->user->authorize('com_content''edit''content''own')); ?>
    <?php 
    if ($this->item->state == 0) : ?>
    <div class="system-unpublished">
    <?php endif; ?>

    <?php if ($this->item->params->get('show_title') || $this->item->params->get('show_pdf_icon') || $this->item->params->get('show_print_icon') || $this->item->params->get('show_email_icon') || $canEdit) : ?>
    <table class="contentpaneopen<?php echo $this->escape($this->item->params->get'pageclass_sfx' )); ?>">
    <tr>
        <?php if ($this->item->params->get('show_title')) : ?>
        <td class="contentheading<?php echo $this->escape($this->item->params->get'pageclass_sfx' )); ?>" width="100%">
            <?php if ($this->item->params->get('link_titles') && $this->item->readmore_link != '') : ?>
            <a href="<?php echo $this->item->readmore_link?>" class="contentpagetitle<?php echo $this->escape($this->item->params->get'pageclass_sfx' )); ?>">
                <?php echo $this->escape($this->item->title); ?></a>
            <?php else : ?>
                <?php echo $this->escape($this->item->title); ?>
            <?php endif; ?>
        </td>
        <?php endif; ?>

        <?php if ($this->item->params->get('show_pdf_icon')) : ?>
        <td align="right" width="100%" class="buttonheading">
        <?php echo JHTML::_('icon.pdf'$this->item$this->item->params$this->access); ?>
        </td>
        <?php endif; ?>

        <?php if ( $this->item->params->get'show_print_icon' )) : ?>
        <td align="right" width="100%" class="buttonheading">
        <?php echo JHTML::_('icon.print_popup'$this->item$this->item->params$this->access); ?>
        </td>
        <?php endif; ?>

        <?php if ($this->item->params->get('show_email_icon')) : ?>
        <td align="right" width="100%" class="buttonheading">
        <?php echo JHTML::_('icon.email'$this->item$this->item->params$this->access); ?>
        </td>
        <?php endif; ?>
        
        <?php if ($canEdit) : ?>
           <td align="right" width="100%" class="buttonheading">
           <?php echo JHTML::_('icon.edit'$this->item$this->item->params$this->access); ?>
           </td>
       <?php endif; ?>
        
    </tr>
    </table>
    <?php endif; ?>
    <?php  
    if (!$this->item->params->get('show_intro')) :
        echo 
    $this->item->event->afterDisplayTitle;
    endif; 
    ?>
    <?php 
    echo $this->item->event->beforeDisplayContent?>
    <table class="contentpaneopen<?php echo $this->escape($this->item->params->get'pageclass_sfx' )); ?>">
    <?php if (($this->item->params->get('show_section') && $this->item->sectionid) || ($this->item->params->get('show_category') && $this->item->catid)) : ?>
    <tr>
        <td>
            <?php if ($this->item->params->get('show_section') && $this->item->sectionid && isset($this->section->title)) : ?>
            <span>
                <?php if ($this->item->params->get('link_section')) : ?>
                    <?php echo '<a href="'.JRoute::_(ContentHelperRoute::getSectionRoute($this->item->sectionid)).'">'?>
                <?php endif; ?>
                <?php echo $this->escape($this->section->title); ?>
                <?php if ($this->item->params->get('link_section')) : ?>
                    <?php echo '</a>'?>
                <?php endif; ?>
                    <?php if ($this->item->params->get('show_category')) : ?>
                    <?php echo ' - '?>
                <?php endif; ?>
            </span>
            <?php endif; ?>
            <?php if ($this->item->params->get('show_category') && $this->item->catid) : ?>
            <span>
                <?php if ($this->item->params->get('link_category')) : ?>
                    <?php echo '<a href="'.JRoute::_(ContentHelperRoute::getCategoryRoute($this->item->catslug$this->item->sectionid)).'">'?>
                <?php endif; ?>
                <?php echo $this->escape($this->item->category); ?>
                <?php if ($this->item->params->get('link_category')) : ?>
                    <?php echo '</a>'?>
                <?php endif; ?>
            </span>
            <?php endif; ?>
        </td>
    </tr>
    <?php endif; ?>

    <?php if (($this->item->params->get('show_author')) && ($this->item->author != "")) : ?>
    <tr>
        <td width="70%"  valign="top" colspan="2">
            <span class="small">
                <?php JText::printf'Written by', ($this->escape($this->item->created_by_alias) ? $this->escape($this->item->created_by_alias) : $this->escape($this->item->author)) ); ?>
            </span>
            &nbsp;&nbsp;
        </td>
    </tr>
    <?php endif; ?>

    <?php if ($this->item->params->get('show_create_date')) : ?>
    <tr>
        <td valign="top" colspan="2" class="createdate">
            <?php echo JHTML::_('date'$this->item->createdJText::_('DATE_FORMAT_LC2')); ?>
        </td>
    </tr>
    <?php endif; ?>

    <?php if ($this->item->params->get('show_url') && $this->item->urls) : ?>
    <tr>
        <td valign="top" colspan="2">
            <a href="http://<?php echo $this->escape($this->item->urls) ; ?>" target="_blank">
                <?php echo $this->escape($this->item->urls); ?></a>
        </td>
    </tr>
    <?php endif; ?>

    <tr>
    <td valign="top" colspan="2">
    <?php if (isset ($this->item->toc)) : ?>
        <?php echo $this->item->toc?>
    <?php 
    endif; ?>
    <?php 
    echo $this->item->text?>
    </td>
    </tr>

    <?php if ( intval($this->item->modified) != && $this->item->params->get('show_modify_date')) : ?>
    <tr>
        <td colspan="2"  class="modifydate">
            <?php echo JText::sprintf('LAST_UPDATED2'JHTML::_('date'$this->item->modifiedJText::_('DATE_FORMAT_LC2'))); ?>
        </td>
    </tr>
    <?php endif; ?>

    <?php if ($this->item->params->get('show_readmore') && $this->item->readmore) : ?>
    <tr>
        <td  colspan="2">
            <a href="<?php echo $this->item->readmore_link?>" class="readon<?php echo $this->escape($this->item->params->get('pageclass_sfx')); ?>">
                <?php if ($this->item->readmore_register) :
                    echo 
    JText::_('Register to read more...');
                elseif (
    $readmore $this->item->params->get('readmore')) :
                    echo 
    $readmore;
                else :
                    echo 
    JText::sprintf('Xem chi tiết...');
                endif; 
    ?></a>
        </td>
    </tr>
    <?php endif; ?>

    </table>
    <?php if ($this->item->state == 0) : ?>
    </div>
    <?php endif; ?>
    <span class="article_separator">&nbsp;</span>
    <?php echo $this->item->event->afterDisplayContent?>
  2. nhoccon

    nhoccon Thành viên mới

    <td class="contentheading<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>" width="100%">
    <?php if ($this->item->params->get('link_titles') && $this->item->readmore_link != '') : ?>
    <a href="<?php echo $this->item->readmore_link; ?>" class="contentpagetitle<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>">
    <?php if (count($this->escape($this->item->title))>50) echo substr($this->escape($this->item->title), 0, 49)."..."; ?></a>
    <?php else : ?>
    <?php if (count($this->escape($this->item->title))>50) echo substr($this->escape($this->item->title), 0, 49)."..."; ?>
    <?php endif; ?>
    </td>


    Thử xem đúng ko, em chỉ nghĩ ý tưởng, chưa test @_@.
    Miso thích bài này.
  3. jelyo

    jelyo Thành viên mới

    uk thanks bạn nhé. để mình test coi ^^
  4. jelyo

    jelyo Thành viên mới

    Nếu thêm code này vào thì title bài viết ở section không hiện bạn à. du sao vẫn cảm ơn bạn rất nhiều.^^
  5. jelyo

    jelyo Thành viên mới

    bạn nào biết thì vào giúp mình với minh rất cần cái nè.
  6. nhoccon

    nhoccon Thành viên mới

    À mình giờ mới nhớ ra thiếu mất in ra nếu dưới 50 kí tự:
    <?php // no direct access
    defined('_JEXEC') or die('Restricted access'); ?>
    <?php $canEdit = ($this->user->authorize('com_content', 'edit', 'content', 'all') || $this->user->authorize('com_content', 'edit', 'content', 'own')); ?>
    <?php if ($this->item->state == 0) : ?>
    <div class="system-unpublished">
    <?php endif; ?>
    <?php if (strlen($this->escape($this->item->title))>50) $title=substr($this->escape($this->item->title), 0, 49)."...";
    else $title=$this->escape($this->item->title);
    ?>
    <?php if ($this->item->params->get('show_title') || $this->item->params->get('show_pdf_icon') || $this->item->params->get('show_print_icon') || $this->item->params->get('show_email_icon') || $canEdit) : ?>
    <table class="contentpaneopen<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>">
    <tr>
    <?php if ($this->item->params->get('show_title')) : ?>
    <td class="contentheading<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>" width="100%">
    <?php if ($this->item->params->get('link_titles') && $this->item->readmore_link != '') : ?>
    <a href="<?php echo $this->item->readmore_link; ?>" class="contentpagetitle<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>">
    <?php echo $title; ?></a>
    <?php else : ?>
    <?php echo $title; ?>
    <?php endif; ?>
    </td>
    Misojelyo thích bài này.
  7. jelyo

    jelyo Thành viên mới

    mình test rồi nó hiện ra nhưng khi mình để title dài hơn 50 kí tự thì nó vẫn vậy bạn à. cám ơn bạn đã giúp nhưng bạn xem lại hộ mình tí đc ko. hoặc bạn wa teamview giúp mình tẹo nhé. Y!h bạn là j để mình add
  8. nhoccon

    nhoccon Thành viên mới

    Sorry vì không test. Em cũng không biết template của bác là template gì, có thể phải sửa phần override trong template.
    Cái sai của em :
    - Dùng sai lệnh, count -> strlen để đếm số kí tự của chuỗi title.
    - Chưa tính trường hợp số kí tự nhỏ hơn 50.
    Hàm trong <?php ?> sau chữ "endif" đầu tiên để gán $title cho chuỗi title của aticle cần in ra.
    Em đã test thành công override lên file default ở đường dẫn: /thu_muc_joomla/templates/ten template/html/aticle/default.php.
    Code:
    <?php

    // no direct access
    defined('_JEXEC') or die('Restricted access');
    include_once(dirname(__FILE__).DS.'..'.DS.'icon.php');

    $canEdit = ($this->user->authorize('com_content', 'edit', 'content', 'all') || $this->user->authorize('com_content', 'edit', 'content', 'own'));
    ?>

    <div class="rt-joomla <?php echo $this->escape($this->params->get('pageclass_sfx')); ?>">

    <div class="rt-article">

    <?php /** Begin Page Title **/ if ($this->params->get('show_page_title', 1) && $this->params->get('page_title') != $this->article->title) : ?>
    <h1 class="rt-pagetitle">
    <?php echo $this->escape($this->params->get('page_title')); ?>
    </h1>
    <?php /** End Page Title **/ endif; ?>
    <?php if (strlen($this->escape($this->article->title))>50)
    $title=substr($this->escape($this->article->title), 0, 49)."...";
    else $title=$this->escape($this->article->title);
    ?>
    <?php /** Begin Article Title **/ if ($canEdit || $this->params->get('show_title')) : ?>
    <div class="rt-headline">
    <?php if ($this->params->get('show_title')) : ?>
    <h1 class="rt-article-title">
    <?php if ($this->params->get('link_titles') && $this->article->readmore_link != '') : ?>
    <a href="<?php echo $this->article->readmore_link; ?>"><?php echo $title; ?></a>
    <?php else : ?>
    <?php echo $title; ?>
    <?php endif; ?>
    </h1>
    <?php endif; ?>
    <?php if (!$this->print) : ?>
    <?php if ($canEdit) : ?>
    <span class="icon edit">
    <?php echo JHTML::_('icon.edit', $this->article, $this->params, $this->access); ?>
    </span>
    <?php endif; ?>
    <?php else : ?>
    <span class="icon printscreen">
    <?php echo JHTML::_('icon.print_screen', $this->article, $this->params, $this->access); ?>
    </span>
    <?php endif; ?>
    <div class="clear"></div>
    </div>
    <?php /** End Article Title **/ endif; ?>

    <?php if (!$this->params->get('show_intro')) :
    echo $this->article->event->afterDisplayTitle;
    endif; ?>

    <?php echo $this->article->event->beforeDisplayContent; ?>

    <?php if ((intval($this->article->modified) !=0 && $this->params->get('show_modify_date')) || ($this->params->get('show_author') && ($this->article->author != "")) || ($this->params->get('show_create_date')) || ($this->params->get('show_pdf_icon') || $this->params->get('show_print_icon') || $this->params->get('show_email_icon'))) : ?>
    <div class="rt-articleinfo">
    <?php /** Begin Article Icons **/ if ($this->params->get('show_pdf_icon') || $this->params->get('show_print_icon') || $this->params->get('show_email_icon')) : ?>
    <div class="rt-article-icons">
    <?php if ($this->print) :
    echo RokIcon::print_screen($this->article, $this->params, $this->access);
    elseif ($this->params->get('show_pdf_icon') || $this->params->get('show_print_icon') || $this->params->get('show_email_icon')) : ?>
    <?php if ($this->params->get('show_pdf_icon')) :
    echo RokIcon::pdf($this->article, $this->params, $this->access);
    endif;
    if ($this->params->get('show_print_icon')) :
    echo RokIcon::print_popup($this->article, $this->params, $this->access);
    endif;
    if ($this->params->get('show_email_icon')) :
    echo RokIcon::email($this->article, $this->params, $this->access);
    endif;
    endif; ?>
    </div>
    <?php /** End Article Icons **/ endif; ?>

    <?php /** Begin Created Date **/ if ($this->params->get('show_create_date')) : ?>
    <span class="rt-date-posted">
    <?php echo JHTML::_('date', $this->article->created, JText::_('DATE_FORMAT_LC2')) ?>
    </span>
    <?php /** End Created Date **/ endif; ?>

    <?php /** Begin Modified Date **/ if (intval($this->article->modified) !=0 && $this->params->get('show_modify_date')) : ?>
    <span class="rt-date-modified">
    <?php echo JText::sprintf('LAST_UPDATED2', JHTML::_('date', $this->article->modified, JText::_('DATE_FORMAT_LC2'))); ?>
    </span>
    <?php /** End Modified Date **/ endif; ?>

    <?php /** Begin Author **/ if ($this->params->get('show_author') && ($this->article->author != "")) : ?>
    <span class="rt-author">
    <?php JText::printf( 'Written by', ($this->escape($this->article->created_by_alias) ? $this->escape($this->article->created_by_alias) : $this->escape($this->article->author)) ); ?>
    </span>
    <?php /** End Author **/endif; ?>

    <?php /** Begin Url **/ if ($this->params->get('show_url') && $this->article->urls) : ?>
    <span class="rt-url">
    <a href="http://<?php echo $this->article->urls ; ?>" target="_blank"><?php echo $this->escape($this->article->urls); ?></a>
    </span>
    <?php /** End Url **/ endif; ?>
    </div>
    <?php endif; ?>

    <?php if (isset ($this->article->toc)) : ?>
    <?php echo $this->article->toc; ?>
    <?php endif; ?>

    <?php echo $this->article->text; ?>

    <?php echo $this->article->event->afterDisplayContent; ?>
    <?php /** Begin Article Sec/Cat **/ if (($this->params->get('show_section') && $this->article->sectionid) || ($this->params->get('show_category') && $this->article->catid)) : ?>
    <p class="rt-article-cat">
    <?php if ($this->params->get('show_section') && $this->article->sectionid && isset($this->article->section)) : ?>
    <span class="rt-section">
    <?php if ($this->params->get('link_section')) : ?>
    <?php echo '<a href="'.JRoute::_(ContentHelperRoute::getSectionRoute($this->article->sectionid)).'">'; ?>
    <?php endif; ?>
    <?php echo $this->escape($this->article->section); ?>
    <?php if ($this->params->get('link_section')) : ?>
    <?php echo '</a>'; ?>
    <?php endif; ?>
    <?php if ($this->params->get('show_category')) : ?>
    <?php echo ' - '; ?>
    <?php endif; ?>
    </span>
    <?php endif; ?>
    <?php if ($this->params->get('show_category') && $this->article->catid) : ?>
    <span class="rt-category">
    <?php if ($this->params->get('link_category')) : ?>
    <?php echo '<a href="'.JRoute::_(ContentHelperRoute::getCategoryRoute($this->article->catslug, $this->article->sectionid)).'">'; ?>
    <?php endif; ?>
    <?php echo $this->escape($this->article->category); ?>
    <?php if ($this->params->get('link_category')) : ?>
    <?php echo '</a>'; ?>
    <?php endif; ?>
    </span>
    <?php endif; ?>
    </p>
    <?php /** End Article Sec/Cat **/ endif; ?>

    </div>

    </div>
  9. nhoccon

    nhoccon Thành viên mới

    Yahoo của em: nhoccondalonroi
  10. jelyo

    jelyo Thành viên mới

    Thanks anh nhé. làm đc rồi hề hề
    Miso thích bài này.

Chia sẻ trang này