如何限制特色文章标题的特征

时间:2013-06-05 18:20:54

标签: joomla2.5

我需要在主页(特色文章)中只有一行文章标题。 因此,需要标题不超过26个字符。剩下的字符将被删除并替换为......

我该怎么做?

3 个答案:

答案 0 :(得分:0)

只需子串以确保足够的长度。我不知道Joomla,但C#等价物将是:

private string FormatForTitle(string input)
{
    if (input.Length <= 26)
    {
        return input;
    }

    // Trim
    return input.Substring(0, 23) + "...";
}

不应该很难移植到PHP。

答案 1 :(得分:0)

FooThis是通过插件实现的。以下应该做的工作 - 请。压缩这些文件并使用扩展名mgr安装。

foo.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="1.6" method="upgrade" group="content">
<name>Content - foo</name>
<creationDate>2013-05-30</creationDate>
<version>1.0</version>
<releaseDate>2013-06-07 07:08:00</releaseDate>
<releaseType>First public release!</releaseType>
<author>Michael Baas</author>
<authorEmail>mb@mbaas.de</authorEmail>
<authorUrl>mbaas.de</authorUrl>
<copyright>(c) 2013 Michael Baas</copyright>
<description>Limit length of featured article-titles - demo</description>
<files>
<filename plugin="foo">foo.php</filename>
</files>
</extension>

foo.php

<?php
    class plgContentFoo extends JPlugin
        {
        public function onContentPrepare($context,$article,$params,$limitstart)
        {

        $view = JRequest::getCmd( 'view');
        // echo "view=$view";
        $fp = $in_array($view,array("Frontpagecategory","addmore"));

            if ($fp && 26<strlen($article->title)) {
                $article->title = trim(substr($article->title,0,23)) . "...";
                return true;
            }
        }


    }

?>

如果它不适合您,可能是您的首页上有不同的视图。在那种情况下请。从“// echo”中删除评论view = $ view“;”并使用查看首页时显示的结果替换以下行中的“addmore”。 (但要小心:“Frontpagearticle”意味着你正在从首页查看一篇文章,我假设在那种情况下你不想修剪......)

此处还描述了检测用户是否正在查看首页的“常用”方法:http://docs.joomla.org/How_to_determine_if_the_user_is_viewing_the_front_page但该方法的问题是,查看fp上的特色文章也会导致这些问题声明返回“TRUE”,我认为是错误的,因为用户没有查看 首页然后......

然而,话虽如此,可能是我的代码在某些情况下也可能失败,并且需要与我链接的文章(检查菜单)中的方法结合...

答案 2 :(得分:0)

<script type="text/javascript">
//<![CDATA[
$(".PopularPosts .post-title a").each(function(){
    if($(this).text().length>10)
    {$(this).text($(this).text().substr(0, 10)+'...');}
});    
//]]>
</script>