以下是我正在处理的文件中的代码段。我将首先说我试图多次失败,我不是编码员,但我希望我知道更多。我需要一些帮助来弄清楚如何将字符串长度添加到字符串$ 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)." "."\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;
}
答案 0 :(得分:1)
我找不到您在代码中获得帖子标题的位置,但要将字符串截断为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).'...';