将 substr max length 添加到我的函数中

发布于 2024-12-07 20:18:43 字数 4963 浏览 3 评论 0原文

下面是我正在使用的文件中的代码片段。首先我要说的是,我曾多次尝试自己找出答案,但都失败了,我不是程序员,但我希望我知道更多。我需要一些帮助来弄清楚如何向字符串 $forum 添加子字符串长度。

此函数输出最新的 5 个论坛主题。我遇到的问题是主题标题太长,无法显示小部件的放置位置,因此我想将其截断为最多显示 35 个字符。你能帮助我吗?我知道我是新人!

   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_globals($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;
    }

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_globals($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)." "."\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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2024-12-14 20:18:43

我找不到在代码中获取帖子标题的位置,但要将字符串截断为 35 个字符,您只需要类似 $truncated = substr($title, 0, 35)

您可能会发现将 '...' 附加到截断的标题很有帮助:

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


编辑:

不带看到你省略了很多代码,很难说,但我怀疑这是一个改变

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

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

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).'...';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文