开发者

Add substr max length to my function

Below is a code snippet from the file I am working with. I will start by saying I have attempted to find out on my own many times with failure, I am not a coder but I wish I knew more. I need some help figuring out how to add a substr length to the string $forum

This function outputs the latest 5 forum topics. The problem I'm having is the topic titles are to long for where the widget is being placed, so I wanted to truncate it to max 35 characters displayed. Can you help me? I know I'm a newb!

   function sf_recent_posts_tag($limit=5, $forum=false, $user=true, $postdate=false, $listtags=true, $forumids=0, $posttime=false, $avatar=false, $size=25)
    {
        global $wpdb, $current_user, $sfvars;

        $limit = sf_esc_int($limit);
        if (empty($limit)) return;

        sf_initialise_globa开发者_运维百科ls($sfvars['forumid']);

        $out = '';
        $forum_ids = '';

        # are we passing forum ID's?
        if ($forumids != 0)
        {
            $flist = explode(",", $forumids);
            foreach($flist as $thisforum)
            {
                if (sf_can_view_forum($thisforum))
                {
                    $forum_ids[] = $thisforum;
                }
            }
        } else {
            # limit to viewable forums based on permissions
            if($current_user->forumadmin == false)
            {
                $allforums = sf_get_forum_memberships($current_user->ID);
                if ($allforums)
                {
                    foreach ($allforums as $thisforum)
                    {
                        if (sf_can_view_forum($thisforum->forum_id))
                        {
                            $forum_ids[] = $thisforum->forum_id;
                        }
                    }
                } else {
                    return '';
                }
            }
        }

        # get out if nothing to see
        if($current_user->forumadmin == false && empty($forum_ids)) return '';

        # create where clause based on forums that current user can view
        if ($forum_ids != '')
        {
            $where = ' AND '.SFPOSTS.".forum_id IN (" . implode(",", $forum_ids) . ") = 1 ";
        } else {
            $where = '';
        }

        $sfposts = $wpdb->get_results("SELECT DISTINCT topic_id
                                       FROM ".SFPOSTS."
                                       WHERE post_status = 0 ".$where."
                                       ORDER BY post_id DESC
                                       LIMIT ".$limit);

        if($sfposts)
        {
            foreach($sfposts as $sfpost)
            {
                $postdetails = sf_get_last_post_in_topic($sfpost->topic_id);
                $thisforum = sf_get_forum_record($postdetails->forum_id);
                $p=false;

                # Start contruction
                if($listtags) $out.="<li class='sftagli'>\n";

                if ($avatar)
                    {
                    if ($postdetails->user_id)
                    {
                        $icon = 'user';
                        if (sf_is_forum_admin($postdetails->user_id)) $icon='admin';
                    } else {
                        $icon = 'guest';
                    }
                    $out.= sf_render_avatar($icon, $postdetails->user_id, sf_filter_email_display($postdetails->user_email), sf_filter_email_display($postdetails->guestemail), false, $size);
                }

                $out.= sf_get_topic_url_newpost($thisforum->forum_slug, $sfpost->topic_id, $postdetails->post_id, $postdetails->post_index);

                if($forum)
                {
                    if ($p == false) $out.="<p class='sftagp'>";

                    $out.= __("posted in forum", "sforum").' '.sf_filter_title_display($thisforum->forum_name)."&nbsp;"."\n";
                    $p=true;
                }

                if($user)
                {
                    if($p == false) $out.="<p class='sftagp'>";
                    $poster = sf_build_name_display($postdetails->user_id, sf_filter_name_display($postdetails->display_name));
                    if(empty($poster)) $poster = sf_filter_name_display($postdetails->guest_name);
                    $out.=__("by", "sforum").' '.$poster.' '."\n";
                    $p=true;
                }

                if($postdate)
                {
                    if($p == false) $out.="<p class='sftagp'>";
                    $out.=__("on", "sforum").' '.sf_date('d', $postdetails->post_date)."\n";
                    if ($posttime)
                    {
                        $out.=' '.__("at", "sforum").' '.sf_date('t', $postdetails->post_date)."\n";
                    }
                    $p=true;
                }

                if($p) $out.="</p>\n";

                if($listtags) $out.="</li>\n";
            }
        } else {
            if($listtags) $out.="<li class='sftagli'>\n";
            $out.='<p>'.__("No Topics to Display", "sforum").'</p>'."\n";
            if($listtags) $out.="</li>\n";
        }
        echo($out);
        return;
    }


I can't find where you get the post titles in your code, but to truncate a string to 35 chars you simply need something like $truncated = substr($title, 0, 35)

You may find it helpful to append '...' to the truncated titles:

$truncated = substr($title, 0, 35).'...';


EDIT:

without seeing a lot of code that you have omitted it's hard to say, but I suspect it's a matter of changing

$out.= sf_get_topic_url_newpost($thisforum->forum_slug, $sfpost->topic_id, $postdetails->post_id, $postdetails->post_index);

to

$out.= substr(sf_get_topic_url_newpost($thisforum->forum_slug, $sfpost->topic_id, $postdetails->post_id, $postdetails->post_index), 0, 35).'...';

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜