【发布时间】:2010-11-03 02:04:27
【问题描述】:
我开发的主题顶部有一个导航下拉菜单。基本上在导航中有两列,其中一列显示检索到的帖子标题(这很容易)。
然而,我想显示帖子标题和特定帖子的链接,但由于空间限制为大约 40 个字符并且每个链接用竖线分隔,我需要弄清楚如何显示一定数量的帖子标题符合我的字数限制。
基本上,如果帖子标题占用 40 个字符,那么我不想显示任何其他标题,基本上我需要获取所有帖子标题的总长度并计算出哪些可以显示以适应字符限制约束。
我的意思示例以防您还无法理解我要做什么。
社区 帖子标题 |另一个帖子标题
我有以下代码,它可以提取帖子,然后计算标题中的字符总数。在应用了字符约束的情况下,我无法输出由管道分隔的链接。
/* Fetches all post data from the Wordpress DB */
$fetched_posts = array(
'community' => get_posts('numberposts=3&tag=community'),
'communication' => get_posts('numberposts=3&tag=communication'),
'energy' => get_posts('numberposts=3&tag=energy'),
'health' => get_posts('numberposts=3&tag=health'),
'prosperity' => get_posts('numberposts=3&tag=prosperity'),
'simplicity' => get_posts('numberposts=3&tag=simplicity'),
'materials' => get_posts('numberposts=3&tag=materials'),
'mobility' => get_posts('numberposts=3&tag=mobility'),
'aesthetic' => get_posts('numberposts=3&tag=aesthetic')
);
// Convert all array entries into variables
extract($fetched_posts);
/**
* Show menu items will output items from a particular tagged category
* but only as many that will fit in the navigation menu space.
*
* @param mixed $object
* @param mixed $maximum
*/
function show_menu_items($object, $maximum = 40) {
// Number of elements in the array
$total = 0;
// Total number of characters we've counted
$counted = 0;
// Store all of the titles for this particular object
foreach ($object as $object) {
$post_titles[] = $object->post_title;
}
// Store the total number of elements in the array
$total = count($post_titles);
// For every post title found count the characters
foreach ($post_titles as $post_title) {
if (strlen($post_title) )
$counted = $counted + strlen($post_title);
}
echo $counted;
}
【问题讨论】: